jdk/src/share/classes/com/sun/jndi/ldap/LdapClient.java
author coffeys
Thu, 03 Mar 2011 16:51:03 +0000
changeset 8564 d99f879a35ab
parent 5506 202f599c92aa
child 10324 e28265130e4f
permissions -rw-r--r--
6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls 6748156: add an new JNDI property to control the boolean flag WaitForReply Reviewed-by: vinnie, robm
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
8564
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
     2
 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.jndi.ldap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.naming.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.naming.directory.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.naming.ldap.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import com.sun.jndi.ldap.pool.PooledConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import com.sun.jndi.ldap.pool.PoolCallback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import com.sun.jndi.ldap.sasl.LdapSasl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import com.sun.jndi.ldap.sasl.SaslInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * LDAP (RFC-1777) and LDAPv3 (RFC-2251) compliant client
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * This class represents a connection to an LDAP client.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Callers interact with this class at an LDAP operation level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * That is, the caller invokes a method to do a SEARCH or MODRDN
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * operation and gets back the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * The caller uses the constructor to create a connection to the server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * It then needs to use authenticate() to perform an LDAP BIND.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * Note that for v3, BIND is optional so authenticate() might not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * actually send a BIND. authenticate() can be used later on to issue
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * a BIND, for example, for a v3 client that wants to change the connection's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * credentials.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Multiple LdapCtx might share the same LdapClient. For example, contexts
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * derived from the same initial context would share the same LdapClient
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * until changes to a context's properties necessitates its own LdapClient.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * LdapClient methods that access shared data are thread-safe (i.e., caller
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * does not have to sync).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * Fields:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *   isLdapv3 - no sync; initialized and updated within sync authenticate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *       always updated when connection is "quiet" and not shared;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *       read access from outside LdapClient not sync
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *   referenceCount - sync within LdapClient; exception is forceClose() which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *       is used by Connection thread to close connection upon receiving
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *       an Unsolicited Notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *       access from outside LdapClient must sync;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *   conn - no sync; Connection takes care of its own sync
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *   unsolicited - sync Vector; multiple operations sync'ed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * @author Vincent Ryan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * @author Jagane Sundar
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * @author Rosanna Lee
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
public final class LdapClient implements PooledConnection {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    // ---------------------- Constants ----------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private static final int debug = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    static final boolean caseIgnore = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    // Default list of binary attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private static final Hashtable defaultBinaryAttrs = new Hashtable(23,0.75f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        defaultBinaryAttrs.put("userpassword", Boolean.TRUE);      //2.5.4.35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        defaultBinaryAttrs.put("javaserializeddata", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                                                //1.3.6.1.4.1.42.2.27.4.1.8
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        defaultBinaryAttrs.put("javaserializedobject", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                                                // 1.3.6.1.4.1.42.2.27.4.1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        defaultBinaryAttrs.put("jpegphoto", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                                                //0.9.2342.19200300.100.1.60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        defaultBinaryAttrs.put("audio", Boolean.TRUE);  //0.9.2342.19200300.100.1.55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        defaultBinaryAttrs.put("thumbnailphoto", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                                                //1.3.6.1.4.1.1466.101.120.35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        defaultBinaryAttrs.put("thumbnaillogo", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                                                //1.3.6.1.4.1.1466.101.120.36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        defaultBinaryAttrs.put("usercertificate", Boolean.TRUE);     //2.5.4.36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        defaultBinaryAttrs.put("cacertificate", Boolean.TRUE);       //2.5.4.37
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        defaultBinaryAttrs.put("certificaterevocationlist", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                                                //2.5.4.39
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        defaultBinaryAttrs.put("authorityrevocationlist", Boolean.TRUE); //2.5.4.38
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        defaultBinaryAttrs.put("crosscertificatepair", Boolean.TRUE);    //2.5.4.40
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        defaultBinaryAttrs.put("photo", Boolean.TRUE);   //0.9.2342.19200300.100.1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        defaultBinaryAttrs.put("personalsignature", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                                                //0.9.2342.19200300.100.1.53
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        defaultBinaryAttrs.put("x500uniqueidentifier", Boolean.TRUE); //2.5.4.45
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private static final String DISCONNECT_OID = "1.3.6.1.4.1.1466.20036";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    // ----------------------- instance fields ------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    boolean isLdapv3;         // Used by LdapCtx
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    int referenceCount = 1;   // Used by LdapCtx for check for sharing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    Connection conn;  // Connection to server; has reader thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                      // used by LdapCtx for StartTLS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    final private PoolCallback pcb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    final private boolean pooled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private boolean authenticateCalled = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    ////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    // constructor: Create an authenticated connection to server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    ////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    LdapClient(String host, int port, String socketFactory,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        int connectTimeout, int readTimeout, OutputStream trace, PoolCallback pcb)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (debug > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            System.err.println("LdapClient: constructor called " + host + ":" + port );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        conn = new Connection(this, host, port, socketFactory, connectTimeout, readTimeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            trace);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        this.pcb = pcb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        pooled = (pcb != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    synchronized boolean authenticateCalled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return authenticateCalled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    synchronized LdapResult
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    authenticate(boolean initial, String name, Object pw, int version,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        String authMechanism, Control[] ctls,  Hashtable env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        authenticateCalled = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            NamingException ne = new CommunicationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            ne.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            throw ne;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        switch (version) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        case LDAP_VERSION3_VERSION2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        case LDAP_VERSION3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            isLdapv3 = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        case LDAP_VERSION2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            isLdapv3 = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            throw new CommunicationException("Protocol version " + version +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                " not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        LdapResult res = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        if (authMechanism.equalsIgnoreCase("none") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            authMechanism.equalsIgnoreCase("anonymous")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            // Perform LDAP bind if we are reauthenticating, using LDAPv2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            // supporting failover to LDAPv2, or controls have been supplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            if (!initial ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                (version == LDAP_VERSION2) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                (version == LDAP_VERSION3_VERSION2) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                ((ctls != null) && (ctls.length > 0))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    // anonymous bind; update name/pw for LDAPv2 retry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    res = ldapBind(name=null, (byte[])(pw=null), ctls, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                        false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    if (res.status == LdapClient.LDAP_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                        conn.setBound();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    NamingException ne =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                        new CommunicationException("anonymous bind failed: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                        conn.host + ":" + conn.port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    ne.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    throw ne;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                // Skip LDAP bind for LDAPv3 anonymous bind
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                res = new LdapResult();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                res.status = LdapClient.LDAP_SUCCESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        } else if (authMechanism.equalsIgnoreCase("simple")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            // simple authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            byte[] encodedPw = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                encodedPw = encodePassword(pw, isLdapv3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                res = ldapBind(name, encodedPw, ctls, null, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                if (res.status == LdapClient.LDAP_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    conn.setBound();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                NamingException ne =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    new CommunicationException("simple bind failed: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                        conn.host + ":" + conn.port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                ne.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                throw ne;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                // If pw was copied to a new array, clear that array as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                // a security precaution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                if (encodedPw != pw && encodedPw != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    for (int i = 0; i < encodedPw.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                        encodedPw[i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        } else if (isLdapv3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            // SASL authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                res = LdapSasl.saslBind(this, conn, conn.host, name, pw,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                    authMechanism, env, ctls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                if (res.status == LdapClient.LDAP_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                    conn.setBound();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                NamingException ne =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    new CommunicationException("SASL bind failed: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    conn.host + ":" + conn.port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                ne.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                throw ne;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            throw new AuthenticationNotSupportedException(authMechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        // re-try login using v2 if failing over
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        if (initial &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            (res.status == LdapClient.LDAP_PROTOCOL_ERROR) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            (version == LdapClient.LDAP_VERSION3_VERSION2) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            (authMechanism.equalsIgnoreCase("none") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                authMechanism.equalsIgnoreCase("anonymous") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                authMechanism.equalsIgnoreCase("simple"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            byte[] encodedPw = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                isLdapv3 = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                encodedPw = encodePassword(pw, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                res = ldapBind(name, encodedPw, ctls, null, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                if (res.status == LdapClient.LDAP_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    conn.setBound();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                NamingException ne =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    new CommunicationException(authMechanism + ":" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                        conn.host +     ":" + conn.port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                ne.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                throw ne;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                // If pw was copied to a new array, clear that array as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                // a security precaution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                if (encodedPw != pw && encodedPw != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    for (int i = 0; i < encodedPw.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                        encodedPw[i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        // principal name not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        // (map NameNotFoundException to AuthenticationException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        // %%% This is a workaround for Netscape servers returning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        // %%% no such object when the principal name is not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        // %%% Note that when this workaround is applied, it does not allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        // %%% response controls to be recorded by the calling context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        if (res.status == LdapClient.LDAP_NO_SUCH_OBJECT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            throw new AuthenticationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                getErrorMessage(res.status, res.errorMessage));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        conn.setV3(isLdapv3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * Sends an LDAP Bind request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * Cannot be private; called by LdapSasl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @param dn The possibly null DN to use in the BIND request. null if anonymous.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @param toServer The possibly null array of bytes to send to the server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @param auth The authentication mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    synchronized public LdapResult ldapBind(String dn, byte[]toServer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        Control[] bindCtls, String auth, boolean pauseAfterReceipt)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        throws java.io.IOException, NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        // flush outstanding requests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        conn.abandonOutstandingReqs(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        BerEncoder ber = new BerEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        int curMsgId = conn.getMsgId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        LdapResult res = new LdapResult();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        res.status = LDAP_OPERATIONS_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        // build the bind request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        ber.beginSeq(Ber.ASN_SEQUENCE | Ber.ASN_CONSTRUCTOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            ber.encodeInt(curMsgId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            ber.beginSeq(LdapClient.LDAP_REQ_BIND);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                ber.encodeInt(isLdapv3 ? LDAP_VERSION3 : LDAP_VERSION2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                ber.encodeString(dn, isLdapv3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                // if authentication mechanism specified, it is SASL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                if (auth != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    ber.beginSeq(Ber.ASN_CONTEXT | Ber.ASN_CONSTRUCTOR | 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                        ber.encodeString(auth, isLdapv3);    // SASL mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                        if (toServer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                            ber.encodeOctetString(toServer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                                Ber.ASN_OCTET_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    ber.endSeq();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    if (toServer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                        ber.encodeOctetString(toServer, Ber.ASN_CONTEXT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                        ber.encodeOctetString(null, Ber.ASN_CONTEXT, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            ber.endSeq();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            // Encode controls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            if (isLdapv3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                encodeControls(ber, bindCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        ber.endSeq();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        LdapRequest req = conn.writeRequest(ber, curMsgId, pauseAfterReceipt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        if (toServer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            ber.reset();        // clear internally-stored password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        // Read reply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        BerDecoder rber = conn.readReply(req);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        rber.parseSeq(null);    // init seq
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        rber.parseInt();        // msg id
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        if (rber.parseByte() !=  LDAP_REP_BIND) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        rber.parseLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        parseResult(rber, res, isLdapv3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        // handle server's credentials (if present)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        if (isLdapv3 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            (rber.bytesLeft() > 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            (rber.peekByte() == (Ber.ASN_CONTEXT | 7))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            res.serverCreds = rber.parseOctetString((Ber.ASN_CONTEXT | 7), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        res.resControls = isLdapv3 ? parseControls(rber) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        conn.removeRequest(req);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * Determines whether SASL encryption/integrity is in progress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * This check is made prior to reauthentication. You cannot reauthenticate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * over an encrypted/integrity-protected SASL channel. You must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * close the channel and open a new one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    boolean usingSaslStreams() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        return (conn.inStream instanceof SaslInputStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    synchronized void incRefCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        ++referenceCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        if (debug > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            System.err.println("LdapClient.incRefCount: " + referenceCount + " " + this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * Returns the encoded password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    private static byte[] encodePassword(Object pw, boolean v3) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        if (pw instanceof char[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            pw = new String((char[])pw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        if (pw instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            if (v3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                return ((String)pw).getBytes("UTF8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                return ((String)pw).getBytes("8859_1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            return (byte[])pw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    synchronized void close(Control[] reqCtls, boolean hardClose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        --referenceCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        if (debug > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            System.err.println("LdapClient: " + this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            System.err.println("LdapClient: close() called: " + referenceCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            (new Throwable()).printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        if (referenceCount <= 0 && conn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            if (debug > 0) System.err.println("LdapClient: closed connection " + this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            if (!pooled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                // Not being pooled; continue with closing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                conn.cleanup(reqCtls, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                conn = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                // Pooled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                // Is this a real close or a request to return conn to pool
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                if (hardClose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                    conn.cleanup(reqCtls, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                    conn = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    pcb.removePooledConnection(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    pcb.releasePooledConnection(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    // NOTE: Should NOT be synchronized otherwise won't be able to close
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    private void forceClose(boolean cleanPool) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        referenceCount = 0; // force closing of connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        if (debug > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            System.err.println("LdapClient: forceClose() of " + this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        if (conn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            if (debug > 0) System.err.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                "LdapClient: forced close of connection " + this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            conn.cleanup(null, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            conn = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            if (cleanPool) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                pcb.removePooledConnection(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    protected void finalize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        if (debug > 0) System.err.println("LdapClient: finalize " + this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        forceClose(pooled);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * Used by connection pooling to close physical connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    synchronized public void closeConnection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        forceClose(false); // this is a pool callback so no need to clean pool
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * Called by Connection.cleanup(). LdapClient should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * notify any unsolicited listeners and removing itself from any pool.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * This is almost like forceClose(), except it doesn't call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * Connection.cleanup() (because this is called from cleanup()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    void processConnectionClosure() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        // Notify listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        if (unsolicited.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            String msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            if (conn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                msg = conn.host + ":" + conn.port + " connection closed";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                msg = "Connection closed";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            notifyUnsolicited(new CommunicationException(msg));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        // Remove from pool
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        if (pooled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            pcb.removePooledConnection(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    ////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    // LDAP search. also includes methods to encode rfc 1558 compliant filters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    ////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    static final int SCOPE_BASE_OBJECT = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    static final int SCOPE_ONE_LEVEL = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    static final int SCOPE_SUBTREE = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    LdapResult search(String dn, int scope, int deref, int sizeLimit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                      int timeLimit, boolean attrsOnly, String attrs[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                      String filter, int batchSize, Control[] reqCtls,
8564
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
   519
                      Hashtable binaryAttrs, boolean waitFirstReply,
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
   520
                      int replyQueueCapacity)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        throws IOException, NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        LdapResult res = new LdapResult();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        BerEncoder ber = new BerEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        int curMsgId = conn.getMsgId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            ber.beginSeq(Ber.ASN_SEQUENCE | Ber.ASN_CONSTRUCTOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                ber.encodeInt(curMsgId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                ber.beginSeq(LDAP_REQ_SEARCH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                    ber.encodeString(dn == null ? "" : dn, isLdapv3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                    ber.encodeInt(scope, LBER_ENUMERATED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                    ber.encodeInt(deref, LBER_ENUMERATED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                    ber.encodeInt(sizeLimit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                    ber.encodeInt(timeLimit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                    ber.encodeBoolean(attrsOnly);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                    Filter.encodeFilterString(ber, filter, isLdapv3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                    ber.beginSeq(Ber.ASN_SEQUENCE | Ber.ASN_CONSTRUCTOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                        ber.encodeStringArray(attrs, isLdapv3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                    ber.endSeq();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                ber.endSeq();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                if (isLdapv3) encodeControls(ber, reqCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            ber.endSeq();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
8564
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
   547
         LdapRequest req =
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
   548
                conn.writeRequest(ber, curMsgId, false, replyQueueCapacity);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
         res.msgId = curMsgId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
         res.status = LdapClient.LDAP_SUCCESS; //optimistic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
         if (waitFirstReply) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
             // get first reply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
             res = getSearchReply(req, batchSize, res, binaryAttrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
         return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * Abandon the search operation and remove it from the message queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    void clearSearchReply(LdapResult res, Control[] ctls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        if (res != null && conn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            // Only send an LDAP abandon operation when clearing the search
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            // reply from a one-level or subtree search.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            LdapRequest req = conn.findRequest(res.msgId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            if (req == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            // OK if req got removed after check; double removal attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            // but otherwise no harm done
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            // Send an LDAP abandon only if the search operation has not yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            // completed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            if (req.hasSearchCompleted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                conn.removeRequest(req);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                conn.abandonRequest(req, ctls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * Retrieve the next batch of entries and/or referrals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    LdapResult getSearchReply(int batchSize, LdapResult res,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        Hashtable binaryAttrs) throws IOException, NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        LdapRequest req;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        if ((req = conn.findRequest(res.msgId)) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        return getSearchReply(req, batchSize, res, binaryAttrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    private LdapResult getSearchReply(LdapRequest req,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        int batchSize, LdapResult res, Hashtable binaryAttrs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        throws IOException, NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        if (batchSize == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            batchSize = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        if (res.entries != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            res.entries.setSize(0); // clear the (previous) set of entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            res.entries =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                new Vector(batchSize == Integer.MAX_VALUE ? 32 : batchSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        if (res.referrals != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            res.referrals.setSize(0); // clear the (previous) set of referrals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        BerDecoder replyBer;    // Decoder for response
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        int seq;                // Request id
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        Attributes lattrs;      // Attribute set read from response
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        Attribute la;           // Attribute read from response
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        String DN;              // DN read from response
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        LdapEntry le;           // LDAP entry representing response
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        int[] seqlen;           // Holder for response length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        int endseq;             // Position of end of response
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        for (int i = 0; i < batchSize;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            replyBer = conn.readReply(req);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            // process search reply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            replyBer.parseSeq(null);                    // init seq
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            replyBer.parseInt();                        // req id
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            seq = replyBer.parseSeq(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            if (seq == LDAP_REP_SEARCH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                // handle LDAPv3 search entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                lattrs = new BasicAttributes(caseIgnore);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                DN = replyBer.parseString(isLdapv3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                le = new LdapEntry(DN, lattrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                seqlen = new int[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                replyBer.parseSeq(seqlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                endseq = replyBer.getParsePosition() + seqlen[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                while ((replyBer.getParsePosition() < endseq) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                    (replyBer.bytesLeft() > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                    la = parseAttribute(replyBer, binaryAttrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                    lattrs.put(la);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                le.respCtls = isLdapv3 ? parseControls(replyBer) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                res.entries.addElement(le);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            } else if ((seq == LDAP_REP_SEARCH_REF) && isLdapv3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                // handle LDAPv3 search reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                Vector URLs = new Vector(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                // %%% Although not strictly correct, some LDAP servers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                //     encode the SEQUENCE OF tag in the SearchResultRef
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                if (replyBer.peekByte() ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                    (Ber.ASN_SEQUENCE | Ber.ASN_CONSTRUCTOR)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                    replyBer.parseSeq(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                while ((replyBer.bytesLeft() > 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                    (replyBer.peekByte() == Ber.ASN_OCTET_STR)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                    URLs.addElement(replyBer.parseString(isLdapv3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                if (res.referrals == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                    res.referrals = new Vector(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                res.referrals.addElement(URLs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                res.resControls = isLdapv3 ? parseControls(replyBer) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                // Save referral and continue to get next search result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            } else if (seq == LDAP_REP_EXTENSION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                parseExtResponse(replyBer, res); //%%% ignore for now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            } else if (seq == LDAP_REP_RESULT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                parseResult(replyBer, res, isLdapv3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                res.resControls = isLdapv3 ? parseControls(replyBer) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                conn.removeRequest(req);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                return res;     // Done with search
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    private Attribute parseAttribute(BerDecoder ber, Hashtable binaryAttrs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        int len[] = new int[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        int seq = ber.parseSeq(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        String attrid = ber.parseString(isLdapv3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        boolean hasBinaryValues = isBinaryValued(attrid, binaryAttrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        Attribute la = new LdapAttribute(attrid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        if ((seq = ber.parseSeq(len)) == LBER_SET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            int attrlen = len[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            while (ber.bytesLeft() > 0 && attrlen > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                    attrlen -= parseAttributeValue(ber, la, hasBinaryValues);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                    ber.seek(attrlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            // Skip the rest of the sequence because it is not what we want
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            ber.seek(len[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        return la;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    // returns number of bytes that were parsed. Adds the values to attr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    private int parseAttributeValue(BerDecoder ber, Attribute la,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        boolean hasBinaryValues) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        int len[] = new int[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        if (hasBinaryValues) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            la.add(ber.parseOctetString(ber.peekByte(), len));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            la.add(ber.parseStringWithTag(Ber.ASN_SIMPLE_STRING, isLdapv3, len));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        return len[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    private boolean isBinaryValued(String attrid, Hashtable binaryAttrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        String id = attrid.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        return ((id.indexOf(";binary") != -1) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            defaultBinaryAttrs.containsKey(id) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            ((binaryAttrs != null) && (binaryAttrs.containsKey(id))));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    // package entry point; used by Connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    static void parseResult(BerDecoder replyBer, LdapResult res, boolean isLdapv3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        res.status = replyBer.parseEnumeration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        res.matchedDN = replyBer.parseString(isLdapv3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        res.errorMessage = replyBer.parseString(isLdapv3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        // handle LDAPv3 referrals (if present)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        if (isLdapv3 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            (replyBer.bytesLeft() > 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            (replyBer.peekByte() == LDAP_REP_REFERRAL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            Vector URLs = new Vector(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            int[] seqlen = new int[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            replyBer.parseSeq(seqlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            int endseq = replyBer.getParsePosition() + seqlen[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            while ((replyBer.getParsePosition() < endseq) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                (replyBer.bytesLeft() > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                URLs.addElement(replyBer.parseString(isLdapv3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            if (res.referrals == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                res.referrals = new Vector(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            res.referrals.addElement(URLs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    // package entry point; used by Connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    static Vector parseControls(BerDecoder replyBer) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        // handle LDAPv3 controls (if present)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        if ((replyBer.bytesLeft() > 0) && (replyBer.peekByte() == LDAP_CONTROLS)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            Vector ctls = new Vector(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            String controlOID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            boolean criticality = false; // default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            byte[] controlValue = null;  // optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            int[] seqlen = new int[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            replyBer.parseSeq(seqlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            int endseq = replyBer.getParsePosition() + seqlen[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            while ((replyBer.getParsePosition() < endseq) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                (replyBer.bytesLeft() > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                replyBer.parseSeq(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                controlOID = replyBer.parseString(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                if ((replyBer.bytesLeft() > 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                    (replyBer.peekByte() == Ber.ASN_BOOLEAN)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                    criticality = replyBer.parseBoolean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                if ((replyBer.bytesLeft() > 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                    (replyBer.peekByte() == Ber.ASN_OCTET_STR)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                    controlValue =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                        replyBer.parseOctetString(Ber.ASN_OCTET_STR, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                if (controlOID != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                    ctls.addElement(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                        new BasicControl(controlOID, criticality, controlValue));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            return ctls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    private void parseExtResponse(BerDecoder replyBer, LdapResult res)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        parseResult(replyBer, res, isLdapv3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        if ((replyBer.bytesLeft() > 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            (replyBer.peekByte() == LDAP_REP_EXT_OID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            res.extensionId =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                replyBer.parseStringWithTag(LDAP_REP_EXT_OID, isLdapv3, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        if ((replyBer.bytesLeft() > 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            (replyBer.peekByte() == LDAP_REP_EXT_VAL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            res.extensionValue =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                replyBer.parseOctetString(LDAP_REP_EXT_VAL, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        res.resControls = parseControls(replyBer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
    // Encode LDAPv3 controls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
    static void encodeControls(BerEncoder ber, Control[] reqCtls)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        if ((reqCtls == null) || (reqCtls.length == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        byte[] controlVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        ber.beginSeq(LdapClient.LDAP_CONTROLS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            for (int i = 0; i < reqCtls.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                ber.beginSeq(Ber.ASN_SEQUENCE | Ber.ASN_CONSTRUCTOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                    ber.encodeString(reqCtls[i].getID(), true); // control OID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                    if (reqCtls[i].isCritical()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                        ber.encodeBoolean(true); // critical control
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                    if ((controlVal = reqCtls[i].getEncodedValue()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                        ber.encodeOctetString(controlVal, Ber.ASN_OCTET_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                ber.endSeq();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        ber.endSeq();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * Reads the next reply corresponding to msgId, outstanding on requestBer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * Processes the result and any controls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    private LdapResult processReply(LdapRequest req,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        LdapResult res, int responseType) throws IOException, NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        BerDecoder rber = conn.readReply(req);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        rber.parseSeq(null);    // init seq
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        rber.parseInt();        // msg id
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        if (rber.parseByte() !=  responseType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        rber.parseLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        parseResult(rber, res, isLdapv3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        res.resControls = isLdapv3 ? parseControls(rber) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        conn.removeRequest(req);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        return res;     // Done with operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    ////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    // LDAP modify:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
    //  Modify the DN dn with the operations on attributes attrs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    //  ie, operations[0] is the operation to be performed on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    //  attrs[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
    //          dn - DN to modify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    //          operations - add, delete or replace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    //          attrs - array of Attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    //          reqCtls - array of request controls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
    ////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
    static final int ADD = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    static final int DELETE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
    static final int REPLACE = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    LdapResult modify(String dn, int operations[], Attribute attrs[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                      Control[] reqCtls)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        throws IOException, NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        LdapResult res = new LdapResult();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        res.status = LDAP_OPERATIONS_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        if (dn == null || operations.length != attrs.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        BerEncoder ber = new BerEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        int curMsgId = conn.getMsgId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        ber.beginSeq(Ber.ASN_SEQUENCE | Ber.ASN_CONSTRUCTOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            ber.encodeInt(curMsgId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            ber.beginSeq(LDAP_REQ_MODIFY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                ber.encodeString(dn, isLdapv3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                ber.beginSeq(Ber.ASN_SEQUENCE | Ber.ASN_CONSTRUCTOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                    for (int i = 0; i < operations.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                        ber.beginSeq(Ber.ASN_SEQUENCE | Ber.ASN_CONSTRUCTOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                            ber.encodeInt(operations[i], LBER_ENUMERATED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                            // zero values is not permitted for the add op.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                            if ((operations[i] == ADD) && hasNoValue(attrs[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                                throw new InvalidAttributeValueException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                                    "'" + attrs[i].getID() + "' has no values.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                                encodeAttribute(ber, attrs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                        ber.endSeq();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                ber.endSeq();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            ber.endSeq();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            if (isLdapv3) encodeControls(ber, reqCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        ber.endSeq();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        LdapRequest req = conn.writeRequest(ber, curMsgId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        return processReply(req, res, LDAP_REP_MODIFY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
    private void encodeAttribute(BerEncoder ber, Attribute attr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        throws IOException, NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        ber.beginSeq(Ber.ASN_SEQUENCE | Ber.ASN_CONSTRUCTOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            ber.encodeString(attr.getID(), isLdapv3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            ber.beginSeq(Ber.ASN_SEQUENCE | Ber.ASN_CONSTRUCTOR | 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                NamingEnumeration enum_ = attr.getAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                Object val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                while (enum_.hasMore()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                    val = enum_.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                    if (val instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                        ber.encodeString((String)val, isLdapv3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                    } else if (val instanceof byte[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                        ber.encodeOctetString((byte[])val, Ber.ASN_OCTET_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                    } else if (val == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                        // no attribute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                        throw new InvalidAttributeValueException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                            "Malformed '" + attr.getID() + "' attribute value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
            ber.endSeq();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        ber.endSeq();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
    private static boolean hasNoValue(Attribute attr) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        return attr.size() == 0 || (attr.size() == 1 && attr.get() == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    ////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    // LDAP add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
    //          Adds entry to the Directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    ////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
    LdapResult add(LdapEntry entry, Control[] reqCtls)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        throws IOException, NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        LdapResult res = new LdapResult();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        res.status = LDAP_OPERATIONS_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        if (entry == null || entry.DN == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        BerEncoder ber = new BerEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        int curMsgId = conn.getMsgId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        Attribute attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            ber.beginSeq(Ber.ASN_SEQUENCE | Ber.ASN_CONSTRUCTOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                ber.encodeInt(curMsgId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                ber.beginSeq(LDAP_REQ_ADD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                    ber.encodeString(entry.DN, isLdapv3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                    ber.beginSeq(Ber.ASN_SEQUENCE | Ber.ASN_CONSTRUCTOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                        NamingEnumeration enum_ = entry.attributes.getAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                        while (enum_.hasMore()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                            attr = (Attribute)enum_.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                            // zero values is not permitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                            if (hasNoValue(attr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                                throw new InvalidAttributeValueException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                                    "'" + attr.getID() + "' has no values.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                                encodeAttribute(ber, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                    ber.endSeq();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                ber.endSeq();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                if (isLdapv3) encodeControls(ber, reqCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            ber.endSeq();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        LdapRequest req = conn.writeRequest(ber, curMsgId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        return processReply(req, res, LDAP_REP_ADD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
    ////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
    // LDAP delete
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
    //          deletes entry from the Directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
    ////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
    LdapResult delete(String DN, Control[] reqCtls)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        throws IOException, NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        LdapResult res = new LdapResult();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        res.status = LDAP_OPERATIONS_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        if (DN == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        BerEncoder ber = new BerEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        int curMsgId = conn.getMsgId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            ber.beginSeq(Ber.ASN_SEQUENCE | Ber.ASN_CONSTRUCTOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                ber.encodeInt(curMsgId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                ber.encodeString(DN, LDAP_REQ_DELETE, isLdapv3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                if (isLdapv3) encodeControls(ber, reqCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            ber.endSeq();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        LdapRequest req = conn.writeRequest(ber, curMsgId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        return processReply(req, res, LDAP_REP_DELETE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
    ////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
    // LDAP modrdn
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
    //  Changes the last element of DN to newrdn
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
    //          dn - DN to change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    //          newrdn - new RDN to rename to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
    //          deleteoldrdn - boolean whether to delete old attrs or not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    //          newSuperior - new place to put the entry in the tree
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
    //                        (ignored if server is LDAPv2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
    //          reqCtls - array of request controls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
    ////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
    LdapResult moddn(String DN, String newrdn, boolean deleteOldRdn,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                     String newSuperior, Control[] reqCtls)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        throws IOException, NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        boolean changeSuperior = (newSuperior != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                                  newSuperior.length() > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        LdapResult res = new LdapResult();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        res.status = LDAP_OPERATIONS_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        if (DN == null || newrdn == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        BerEncoder ber = new BerEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        int curMsgId = conn.getMsgId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            ber.beginSeq(Ber.ASN_SEQUENCE | Ber.ASN_CONSTRUCTOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                ber.encodeInt(curMsgId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                ber.beginSeq(LDAP_REQ_MODRDN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                    ber.encodeString(DN, isLdapv3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                    ber.encodeString(newrdn, isLdapv3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                    ber.encodeBoolean(deleteOldRdn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                    if(isLdapv3 && changeSuperior) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                        //System.err.println("changin superior");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                        ber.encodeString(newSuperior, LDAP_SUPERIOR_DN, isLdapv3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                ber.endSeq();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                if (isLdapv3) encodeControls(ber, reqCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
            ber.endSeq();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        LdapRequest req = conn.writeRequest(ber, curMsgId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        return processReply(req, res, LDAP_REP_MODRDN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
    ////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
    // LDAP compare
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
    //  Compare attribute->value pairs in dn
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
    ////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
    LdapResult compare(String DN, String type, String value, Control[] reqCtls)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        throws IOException, NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        LdapResult res = new LdapResult();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        res.status = LDAP_OPERATIONS_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        if (DN == null || type == null || value == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        BerEncoder ber = new BerEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        int curMsgId = conn.getMsgId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
            ber.beginSeq(Ber.ASN_SEQUENCE | Ber.ASN_CONSTRUCTOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                ber.encodeInt(curMsgId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                ber.beginSeq(LDAP_REQ_COMPARE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                    ber.encodeString(DN, isLdapv3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                    ber.beginSeq(Ber.ASN_SEQUENCE | Ber.ASN_CONSTRUCTOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
                        ber.encodeString(type, isLdapv3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                        // replace any escaped characters in the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                        byte[] val = isLdapv3 ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                            value.getBytes("UTF8") : value.getBytes("8859_1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                        ber.encodeOctetString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                            Filter.unescapeFilterValue(val, 0, val.length),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                            Ber.ASN_OCTET_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
                    ber.endSeq();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
                ber.endSeq();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
                if (isLdapv3) encodeControls(ber, reqCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
            ber.endSeq();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        LdapRequest req = conn.writeRequest(ber, curMsgId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        return processReply(req, res, LDAP_REP_COMPARE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
    ////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
    // LDAP extended operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
    ////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
    LdapResult extendedOp(String id, byte[] request, Control[] reqCtls,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        boolean pauseAfterReceipt) throws IOException, NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        LdapResult res = new LdapResult();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        res.status = LDAP_OPERATIONS_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        if (id == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        BerEncoder ber = new BerEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        int curMsgId = conn.getMsgId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
            ber.beginSeq(Ber.ASN_SEQUENCE | Ber.ASN_CONSTRUCTOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
                ber.encodeInt(curMsgId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
                ber.beginSeq(LDAP_REQ_EXTENSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
                    ber.encodeString(id,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
                        Ber.ASN_CONTEXT | 0, isLdapv3);//[0]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
                    if (request != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
                        ber.encodeOctetString(request,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
                            Ber.ASN_CONTEXT | 1);//[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
                ber.endSeq();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
                encodeControls(ber, reqCtls); // always v3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
            ber.endSeq();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        LdapRequest req = conn.writeRequest(ber, curMsgId, pauseAfterReceipt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        BerDecoder rber = conn.readReply(req);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        rber.parseSeq(null);    // init seq
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        rber.parseInt();        // msg id
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        if (rber.parseByte() !=  LDAP_REP_EXTENSION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
        rber.parseLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        parseExtResponse(rber, res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        conn.removeRequest(req);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        return res;     // Done with operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
    ////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
    // Some BER definitions convenient for LDAP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
    ////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
    static final int LDAP_VERSION3_VERSION2 = 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
    static final int LDAP_VERSION2 = 0x02;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
    static final int LDAP_VERSION3 = 0x03;              // LDAPv3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
    static final int LDAP_VERSION = LDAP_VERSION3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
    static final int LDAP_REF_FOLLOW = 0x01;            // follow referrals
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
    static final int LDAP_REF_THROW = 0x02;             // throw referral ex.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
    static final int LDAP_REF_IGNORE = 0x03;            // ignore referrals
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
    static final String LDAP_URL = "ldap://";           // LDAPv3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
    static final String LDAPS_URL = "ldaps://";         // LDAPv3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
    static final int LBER_BOOLEAN = 0x01;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
    static final int LBER_INTEGER = 0x02;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
    static final int LBER_BITSTRING = 0x03;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
    static final int LBER_OCTETSTRING = 0x04;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
    static final int LBER_NULL = 0x05;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
    static final int LBER_ENUMERATED = 0x0a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
    static final int LBER_SEQUENCE = 0x30;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
    static final int LBER_SET = 0x31;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
    static final int LDAP_SUPERIOR_DN = 0x80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
    static final int LDAP_REQ_BIND = 0x60;      // app + constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
    static final int LDAP_REQ_UNBIND = 0x42;    // app + primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
    static final int LDAP_REQ_SEARCH = 0x63;    // app + constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
    static final int LDAP_REQ_MODIFY = 0x66;    // app + constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
    static final int LDAP_REQ_ADD = 0x68;       // app + constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
    static final int LDAP_REQ_DELETE = 0x4a;    // app + primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
    static final int LDAP_REQ_MODRDN = 0x6c;    // app + constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
    static final int LDAP_REQ_COMPARE = 0x6e;   // app + constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
    static final int LDAP_REQ_ABANDON = 0x50;   // app + primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
    static final int LDAP_REQ_EXTENSION = 0x77; // app + constructed    (LDAPv3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
    static final int LDAP_REP_BIND = 0x61;      // app + constructed | 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
    static final int LDAP_REP_SEARCH = 0x64;    // app + constructed | 4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
    static final int LDAP_REP_SEARCH_REF = 0x73;// app + constructed    (LDAPv3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
    static final int LDAP_REP_RESULT = 0x65;    // app + constructed | 5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
    static final int LDAP_REP_MODIFY = 0x67;    // app + constructed | 7
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
    static final int LDAP_REP_ADD = 0x69;       // app + constructed | 9
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
    static final int LDAP_REP_DELETE = 0x6b;    // app + primitive | b
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
    static final int LDAP_REP_MODRDN = 0x6d;    // app + primitive | d
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
    static final int LDAP_REP_COMPARE = 0x6f;   // app + primitive | f
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
    static final int LDAP_REP_EXTENSION = 0x78; // app + constructed    (LDAPv3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
    static final int LDAP_REP_REFERRAL = 0xa3;  // ctx + constructed    (LDAPv3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
    static final int LDAP_REP_EXT_OID = 0x8a;   // ctx + primitive      (LDAPv3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
    static final int LDAP_REP_EXT_VAL = 0x8b;   // ctx + primitive      (LDAPv3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
    // LDAPv3 Controls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
    static final int LDAP_CONTROLS = 0xa0;      // ctx + constructed    (LDAPv3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
    static final String LDAP_CONTROL_MANAGE_DSA_IT = "2.16.840.1.113730.3.4.2";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
    static final String LDAP_CONTROL_PREFERRED_LANG = "1.3.6.1.4.1.1466.20035";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
    static final String LDAP_CONTROL_PAGED_RESULTS = "1.2.840.113556.1.4.319";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
    static final String LDAP_CONTROL_SERVER_SORT_REQ = "1.2.840.113556.1.4.473";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
    static final String LDAP_CONTROL_SERVER_SORT_RES = "1.2.840.113556.1.4.474";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
    ////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
    // return codes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
    ////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
    static final int LDAP_SUCCESS = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
    static final int LDAP_OPERATIONS_ERROR = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
    static final int LDAP_PROTOCOL_ERROR = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
    static final int LDAP_TIME_LIMIT_EXCEEDED = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
    static final int LDAP_SIZE_LIMIT_EXCEEDED = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
    static final int LDAP_COMPARE_FALSE = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
    static final int LDAP_COMPARE_TRUE = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
    static final int LDAP_AUTH_METHOD_NOT_SUPPORTED = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
    static final int LDAP_STRONG_AUTH_REQUIRED = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
    static final int LDAP_PARTIAL_RESULTS = 9;                  // Slapd
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
    static final int LDAP_REFERRAL = 10;                        // LDAPv3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
    static final int LDAP_ADMIN_LIMIT_EXCEEDED = 11;            // LDAPv3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
    static final int LDAP_UNAVAILABLE_CRITICAL_EXTENSION = 12;  // LDAPv3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
    static final int LDAP_CONFIDENTIALITY_REQUIRED = 13;        // LDAPv3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
    static final int LDAP_SASL_BIND_IN_PROGRESS = 14;           // LDAPv3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
    static final int LDAP_NO_SUCH_ATTRIBUTE = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
    static final int LDAP_UNDEFINED_ATTRIBUTE_TYPE = 17;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
    static final int LDAP_INAPPROPRIATE_MATCHING = 18;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
    static final int LDAP_CONSTRAINT_VIOLATION = 19;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
    static final int LDAP_ATTRIBUTE_OR_VALUE_EXISTS = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
    static final int LDAP_INVALID_ATTRIBUTE_SYNTAX = 21;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
    static final int LDAP_NO_SUCH_OBJECT = 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
    static final int LDAP_ALIAS_PROBLEM = 33;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
    static final int LDAP_INVALID_DN_SYNTAX = 34;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
    static final int LDAP_IS_LEAF = 35;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
    static final int LDAP_ALIAS_DEREFERENCING_PROBLEM = 36;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
    static final int LDAP_INAPPROPRIATE_AUTHENTICATION = 48;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
    static final int LDAP_INVALID_CREDENTIALS = 49;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
    static final int LDAP_INSUFFICIENT_ACCESS_RIGHTS = 50;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
    static final int LDAP_BUSY = 51;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
    static final int LDAP_UNAVAILABLE = 52;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
    static final int LDAP_UNWILLING_TO_PERFORM = 53;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
    static final int LDAP_LOOP_DETECT = 54;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
    static final int LDAP_NAMING_VIOLATION = 64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
    static final int LDAP_OBJECT_CLASS_VIOLATION = 65;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
    static final int LDAP_NOT_ALLOWED_ON_NON_LEAF = 66;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
    static final int LDAP_NOT_ALLOWED_ON_RDN = 67;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
    static final int LDAP_ENTRY_ALREADY_EXISTS = 68;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
    static final int LDAP_OBJECT_CLASS_MODS_PROHIBITED = 69;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
    static final int LDAP_AFFECTS_MULTIPLE_DSAS = 71;           // LDAPv3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
    static final int LDAP_OTHER = 80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
    static final String[] ldap_error_message = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
        "Success",                                      // 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
        "Operations Error",                             // 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
        "Protocol Error",                               // 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
        "Timelimit Exceeded",                           // 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
        "Sizelimit Exceeded",                           // 4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
        "Compare False",                                // 5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
        "Compare True",                                 // 6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
        "Authentication Method Not Supported",          // 7
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
        "Strong Authentication Required",               // 8
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        "Referral",                                     // 10
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        "Administrative Limit Exceeded",                // 11
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        "Unavailable Critical Extension",               // 12
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        "Confidentiality Required",                     // 13
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        "SASL Bind In Progress",                        // 14
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
        "No Such Attribute",                            // 16
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        "Undefined Attribute Type",                     // 17
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
        "Inappropriate Matching",                       // 18
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
        "Constraint Violation",                         // 19
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
        "Attribute Or Value Exists",                    // 20
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
        "Invalid Attribute Syntax",                     // 21
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
        "No Such Object",                               // 32
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
        "Alias Problem",                                // 33
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        "Invalid DN Syntax",                            // 34
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        "Alias Dereferencing Problem",                  // 36
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        "Inappropriate Authentication",                 // 48
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
        "Invalid Credentials",                          // 49
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        "Insufficient Access Rights",                   // 50
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
        "Busy",                                         // 51
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
        "Unavailable",                                  // 52
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
        "Unwilling To Perform",                         // 53
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
        "Loop Detect",                                  // 54
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
        "Naming Violation",                             // 64
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
        "Object Class Violation",                       // 65
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
        "Not Allowed On Non-leaf",                      // 66
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
        "Not Allowed On RDN",                           // 67
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
        "Entry Already Exists",                         // 68
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
        "Object Class Modifications Prohibited",        // 69
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
        "Affects Multiple DSAs",                        // 71
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
        "Other",                                        // 80
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
        null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     * Generate an error message from the LDAP error code and error diagnostic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
     * The message format is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
     *     "[LDAP: error code <errorCode> - <errorMessage>]"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     * where <errorCode> is a numeric error code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     * and <errorMessage> is a textual description of the error (if available)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
    static String getErrorMessage(int errorCode, String errorMessage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        String message = "[LDAP: error code " + errorCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
        if ((errorMessage != null) && (errorMessage.length() != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
            // append error message from the server
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
            message = message + " - " + errorMessage + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            // append built-in error message
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
                if (ldap_error_message[errorCode] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
                    message = message + " - " + ldap_error_message[errorCode] +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
                                "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
            } catch (ArrayIndexOutOfBoundsException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
                message = message + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
        return message;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
    ////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
    // Unsolicited notification support.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
    // An LdapClient maintains a list of LdapCtx that have registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
    // for UnsolicitedNotifications. This is a list because a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
    // LdapClient might be shared among multiple contexts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
    // When addUnsolicited() is invoked, the LdapCtx is added to the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
    // When Connection receives an unsolicited notification (msgid == 0),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
    // it invokes LdapClient.processUnsolicited(). processUnsolicited()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
    // parses the Extended Response. If there are registered listeners,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
    // LdapClient creates an UnsolicitedNotification from the response
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
    // and informs each LdapCtx to fire an event for the notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
    // If it is a DISCONNECT notification, the connection is closed and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
    // NamingExceptionEvent is fired to the listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
    // When the connection is closed out-of-band like this, the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
    // time a method is invoked on LdapClient, an IOException is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
    // removeUnsolicited() is invoked to remove an LdapCtx from this client.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
    ////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
    private Vector unsolicited = new Vector(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
    void addUnsolicited(LdapCtx ctx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
        if (debug > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
            System.err.println("LdapClient.addUnsolicited" + ctx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
        unsolicited.addElement(ctx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
    void removeUnsolicited(LdapCtx ctx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
        if (debug > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
            System.err.println("LdapClient.removeUnsolicited" + ctx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
        synchronized (unsolicited) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
            if (unsolicited.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
            unsolicited.removeElement(ctx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
    // NOTE: Cannot be synchronized because this is called asynchronously
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
    // by the reader thread in Connection. Instead, sync on 'unsolicited' Vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
    void processUnsolicited(BerDecoder ber) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
        if (debug > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
            System.err.println("LdapClient.processUnsolicited");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
      synchronized (unsolicited) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
            // Parse the response
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
            LdapResult res = new LdapResult();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
            ber.parseSeq(null); // init seq
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
            ber.parseInt();             // msg id; should be 0; ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
            if (ber.parseByte() != LDAP_REP_EXTENSION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
                throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
                    "Unsolicited Notification must be an Extended Response");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
            ber.parseLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
            parseExtResponse(ber, res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
            if (DISCONNECT_OID.equals(res.extensionId)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
                // force closing of connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
                forceClose(pooled);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
            if (unsolicited.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
                // Create an UnsolicitedNotification using the parsed data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
                // Need a 'ctx' object because we want to use the context's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
                // list of provider control factories.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
                UnsolicitedNotification notice = new UnsolicitedResponseImpl(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
                    res.extensionId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
                    res.extensionValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
                    res.referrals,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
                    res.status,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
                    res.errorMessage,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
                    res.matchedDN,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
                    (res.resControls != null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
            ((LdapCtx)unsolicited.elementAt(0)).convertControls(res.resControls) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
                    null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
                // Fire UnsolicitedNotification events to listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
                notifyUnsolicited(notice);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
                // If "disconnect" notification,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
                // notify unsolicited listeners via NamingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
                if (DISCONNECT_OID.equals(res.extensionId)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
                    notifyUnsolicited(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
                        new CommunicationException("Connection closed"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
            if (unsolicited.size() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
                return;  // no one registered; ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
            NamingException ne = new CommunicationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
                "Problem parsing unsolicited notification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
            ne.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
            notifyUnsolicited(ne);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
        } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
            notifyUnsolicited(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
    private void notifyUnsolicited(Object e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
        for (int i = 0; i < unsolicited.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
            ((LdapCtx)unsolicited.elementAt(i)).fireUnsolicited(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
        if (e instanceof NamingException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
            unsolicited.setSize(0);  // no more listeners after exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
    private void ensureOpen() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
        if (conn == null || !conn.useable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
            if (conn != null && conn.closureReason != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
                throw conn.closureReason;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
                throw new IOException("connection closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
    // package private (used by LdapCtx)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
    static LdapClient getInstance(boolean usePool, String hostname, int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
        String factory, int connectTimeout, int readTimeout, OutputStream trace,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
        int version, String authMechanism, Control[] ctls, String protocol,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
        String user, Object passwd, Hashtable env) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
        if (usePool) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
            if (LdapPoolManager.isPoolingAllowed(factory, trace,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
                    authMechanism, protocol, env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
                LdapClient answer = LdapPoolManager.getLdapClient(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
                        hostname, port, factory, connectTimeout, readTimeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
                        trace, version, authMechanism, ctls, protocol, user,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
                        passwd, env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
                answer.referenceCount = 1;   // always one when starting out
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
                return answer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
        return new LdapClient(hostname, port, factory, connectTimeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
                                        readTimeout, trace, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
}