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