/mcs/class/Novell.Directory.Ldap/Novell.Directory.Ldap/LdapConnection.cs
C# | 3977 lines | 1403 code | 279 blank | 2295 comment | 204 complexity | 7f94a346a1157c05f6fce05a193a72e1 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
Large files files are truncated, but you can click here to view the full file
- /******************************************************************************
- * The MIT License
- * Copyright (c) 2003 Novell Inc. www.novell.com
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the Software), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- *******************************************************************************/
- //
- // Novell.Directory.Ldap.LdapConnection.cs
- //
- // Author:
- // Sunil Kumar (Sunilk@novell.com)
- //
- // (C) 2003 Novell, Inc (http://www.novell.com)
- //
- using System;
- using Novell.Directory.Ldap;
- using Novell.Directory.Ldap.Asn1;
- using Novell.Directory.Ldap.Rfc2251;
- using Novell.Directory.Ldap.Utilclass;
- #if !TARGET_JVM
- using Mono.Security.Protocol.Tls;
- #else
- using org.ietf.jgss;
- using javax.security.auth;
- using javax.security.auth.login;
- using java.security;
- using Novell.Directory.Ldap.Security;
- using System.Collections.Specialized;
- using System.Configuration;
- #endif
- using System.Security.Cryptography.X509Certificates;
- namespace Novell.Directory.Ldap
- {
-
- /// <summary> The central class that encapsulates the connection
- /// to a directory server through the Ldap protocol.
- /// LdapConnection objects are used to perform common Ldap
- /// operations such as search, modify and add.
- ///
- /// In addition, LdapConnection objects allow you to bind to an
- /// Ldap server, set connection and search constraints, and perform
- /// several other tasks.
- ///
- /// An LdapConnection object is not connected on
- /// construction and can only be connected to one server at one
- /// port. Multiple threads may share this single connection, typically
- /// by cloning the connection object, one for each thread. An
- /// application may have more than one LdapConnection object, connected
- /// to the same or different directory servers.
- ///
- ///
- /// </summary>
- public class LdapConnection : System.ICloneable
- {
- private void InitBlock()
- {
- defSearchCons = new LdapSearchConstraints();
- responseCtlSemaphore = new System.Object();
- }
- /// <summary> Returns the protocol version uses to authenticate.
- ///
- /// 0 is returned if no authentication has been performed.
- ///
- /// </summary>
- /// <returns> The protol version used for authentication or 0
- /// not authenticated.
- ///
- /// </returns>
- virtual public int ProtocolVersion
- {
- get
- {
- BindProperties prop = conn.BindProperties;
- if (prop == null)
- {
- return Ldap_V3;
- }
- return prop.ProtocolVersion;
- }
-
- }
- /// <summary> Returns the distinguished name (DN) used for as the bind name during
- /// the last successful bind operation. <code>null</code> is returned
- /// if no authentication has been performed or if the bind resulted in
- /// an aonymous connection.
- ///
- /// </summary>
- /// <returns> The distinguished name if authenticated; otherwise, null.
- ///
- /// </returns>
- virtual public System.String AuthenticationDN
- {
- get
- {
- BindProperties prop = conn.BindProperties;
- if (prop == null)
- {
- return null;
- }
- if (prop.Anonymous)
- {
- return null;
- }
- return prop.AuthenticationDN;
- }
-
- }
- /// <summary> Returns the method used to authenticate the connection. The return
- /// value is one of the following:
- ///
- /// <ul>
- /// <li>"none" indicates the connection is not authenticated.</li>
- ///
- ///
- /// <li>"simple" indicates simple authentication was used or that a null
- /// or empty authentication DN was specified.</li>
- ///
- /// <li>"sasl" indicates that a SASL mechanism was used to authenticate</li>
- /// </ul>
- ///
- /// </summary>
- /// <returns> The method used to authenticate the connection.
- /// </returns>
- virtual public System.String AuthenticationMethod
- {
- get
- {
- BindProperties prop = conn.BindProperties;
- if (prop == null)
- {
- return "simple";
- }
- return conn.BindProperties.AuthenticationMethod;
- }
-
- }
- /// <summary> Returns the properties if any specified on binding with a
- /// SASL mechanism.
- ///
- /// Null is returned if no authentication has been performed
- /// or no authentication Map is present.
- ///
- /// </summary>
- /// <returns> The bind properties Map Object used for SASL bind or null if
- /// the connection is not present or not authenticated.
- ///
- /// </returns>
- virtual public System.Collections.IDictionary SaslBindProperties
- {
- get
- {
- BindProperties prop = conn.BindProperties;
- if (prop == null)
- {
- return null;
- }
- return conn.BindProperties.SaslBindProperties;
- }
-
- }
- /// <summary> Returns the call back handler if any specified on binding with a
- /// SASL mechanism.
- ///
- /// Null is returned if no authentication has been performed
- /// or no authentication call back handler is present.
- ///
- /// </summary>
- /// <returns> The call back handler used for SASL bind or null if the
- /// object is not present or not authenticated.
- ///
- /// </returns>
- virtual public System.Object SaslBindCallbackHandler
- {
- get
- {
- BindProperties prop = conn.BindProperties;
- if (prop == null)
- {
- return null;
- }
- return conn.BindProperties.SaslCallbackHandler;
- }
-
- }
- /// <summary> Returns a copy of the set of constraints associated with this
- /// connection. These constraints apply to all operations performed
- /// through this connection (unless a different set of constraints is
- /// specified when calling an operation method).
- ///
- /// </summary>
- /// <returns> The set of default contraints that apply to this connection.
- ///
- /// </returns>
- /// <summary> Sets the constraints that apply to all operations performed through
- /// this connection (unless a different set of constraints is specified
- /// when calling an operation method). An LdapSearchConstraints object
- /// which is passed to this method sets all constraints, while an
- /// LdapConstraints object passed to this method sets only base constraints.
- ///
- /// </summary>
- /// <param name="cons"> An LdapConstraints or LdapSearchConstraints Object
- /// containing the contstraint values to set.
- ///
- /// </param>
- /// <seealso cref="Constraints()">
- /// </seealso>
- /// <seealso cref="SearchConstraints()">
- /// </seealso>
- virtual public LdapConstraints Constraints
- {
- get
- {
- return (LdapConstraints) (this.defSearchCons).Clone();
- }
-
- set
- {
- // Set all constraints, replace the object with a new one
- if (value is LdapSearchConstraints)
- {
- defSearchCons = (LdapSearchConstraints) value.Clone();
- return ;
- }
-
- // We set the constraints this way, so a thread doesn't get an
- // conconsistant view of the referrals.
- LdapSearchConstraints newCons = (LdapSearchConstraints) defSearchCons.Clone();
- newCons.HopLimit = value.HopLimit;
- newCons.TimeLimit = value.TimeLimit;
- newCons.setReferralHandler(value.getReferralHandler());
- newCons.ReferralFollowing = value.ReferralFollowing;
- LdapControl[] lsc = value.getControls();
- if (lsc != null)
- {
- newCons.setControls(lsc);
- }
- System.Collections.Hashtable lp = newCons.Properties;
- if (lp != null)
- {
- newCons.Properties = lp;
- }
- defSearchCons = newCons;
- return ;
- }
-
- }
- /// <summary> Returns the host name of the Ldap server to which the object is or
- /// was last connected, in the format originally specified.
- ///
- /// </summary>
- /// <returns> The host name of the Ldap server to which the object last
- /// connected or null if the object has never connected.
- ///
- /// </returns>
- virtual public System.String Host
- {
- get
- {
- return conn.Host;
- }
-
- }
- /// <summary> Returns the port number of the Ldap server to which the object is or
- /// was last connected.
- ///
- /// </summary>
- /// <returns> The port number of the Ldap server to which the object last
- /// connected or -1 if the object has never connected.
- ///
- /// </returns>
- virtual public int Port
- {
- get
- {
- return conn.Port;
- }
-
- }
- /// <summary> Returns a copy of the set of search constraints associated with this
- /// connection. These constraints apply to search operations performed
- /// through this connection (unless a different set of
- /// constraints is specified when calling the search operation method).
- ///
- /// </summary>
- /// <returns> The set of default search contraints that apply to
- /// this connection.
- ///
- /// </returns>
- /// <seealso cref="Constraints">
- /// </seealso>
- /// <seealso cref="LdapSearchConstraints">
- /// </seealso>
- virtual public LdapSearchConstraints SearchConstraints
- {
- get
- {
- return (LdapSearchConstraints) this.defSearchCons.Clone();
- }
-
- }
- ///<summary> Indicates whther the perform Secure Operation or not
- ///</summary>
- ///
- ///<returns>
- /// True if SSL is on
- /// False if its not on
- ///</returns>
- public bool SecureSocketLayer
- {
- get
- {
- return conn.Ssl;
- }
- set
- {
- conn.Ssl=value;
- }
- }
- /// <summary> Indicates whether the object has authenticated to the connected Ldap
- /// server.
- ///
- /// </summary>
- /// <returns> True if the object has authenticated; false if it has not
- /// authenticated.
- ///
- /// </returns>
- virtual public bool Bound
- {
- get
- {
- return conn.Bound;
- }
-
- }
- /// <summary> Indicates whether the connection represented by this object is open
- /// at this time.
- ///
- /// </summary>
- /// <returns> True if connection is open; false if the connection is closed.
- /// </returns>
- virtual public bool Connected
- {
- get
- {
- return conn.Connected;
- }
-
- }
- /// <summary> Indicatates if the connection is protected by TLS.
- ///
- /// </summary>
- /// <returns> If startTLS has completed this method returns true.
- /// If stopTLS has completed or start tls failed, this method returns false.
- /// </returns>
- /// <returns> True if the connection is protected by TLS.
- ///
- /// </returns>
- virtual public bool TLS
- {
- get
- {
- return conn.TLS;
- }
-
- }
- /// <summary> Returns the Server Controls associated with the most recent response
- /// to a synchronous request on this connection object, or null
- /// if the latest response contained no Server Controls. The method
- /// always returns null for asynchronous requests. For asynchronous
- /// requests, the response controls are available in LdapMessage.
- ///
- /// </summary>
- /// <returns> The server controls associated with the most recent response
- /// to a synchronous request or null if the response contains no server
- /// controls.
- ///
- /// </returns>
- /// <seealso cref="LdapMessage.Controls">
- /// </seealso>
- virtual public LdapControl[] ResponseControls
- {
- get
- {
- if (responseCtls == null)
- {
- return null;
- }
-
-
- // We have to clone the control just in case
- // we have two client threads that end up retreiving the
- // same control.
- LdapControl[] clonedControl = new LdapControl[responseCtls.Length];
-
- // Also note we synchronize access to the local response
- // control object just in case another message containing controls
- // comes in from the server while we are busy duplicating
- // this one.
- lock (responseCtlSemaphore)
- {
- for (int i = 0; i < responseCtls.Length; i++)
- {
- clonedControl[i] = (LdapControl) (responseCtls[i]).Clone();
- }
- }
-
- // Return the cloned copy. Note we have still left the
- // control in the local responseCtls variable just in case
- // somebody requests it again.
- return clonedControl;
- }
-
- }
- /// <summary> Return the Connection object associated with this LdapConnection
- ///
- /// </summary>
- /// <returns> the Connection object
- /// </returns>
- virtual internal Connection Connection
- {
- /* package */
-
- get
- {
- return conn;
- }
-
- }
- /// <summary> Return the Connection object name associated with this LdapConnection
- ///
- /// </summary>
- /// <returns> the Connection object name
- /// </returns>
- virtual internal System.String ConnectionName
- {
- /* package */
-
- get
- {
- return name;
- }
-
- }
- private LdapSearchConstraints defSearchCons;
- private LdapControl[] responseCtls = null;
-
- // Synchronization Object used to synchronize access to responseCtls
- private System.Object responseCtlSemaphore;
-
- private Connection conn = null;
-
- private static System.Object nameLock; // protect agentNum
- private static int lConnNum = 0; // Debug, LdapConnection number
- private System.String name; // String name for debug
-
- /// <summary> Used with search to specify that the scope of entrys to search is to
- /// search only the base obect.
- ///
- /// SCOPE_BASE = 0
- /// </summary>
- public const int SCOPE_BASE = 0;
-
- /// <summary> Used with search to specify that the scope of entrys to search is to
- /// search only the immediate subordinates of the base obect.
- ///
- /// SCOPE_ONE = 1
- /// </summary>
- public const int SCOPE_ONE = 1;
-
- /// <summary> Used with search to specify that the scope of entrys to search is to
- /// search the base object and all entries within its subtree.
- ///
- /// SCOPE_ONE = 2
- /// </summary>
- public const int SCOPE_SUB = 2;
-
- /// <summary> Used with search instead of an attribute list to indicate that no
- /// attributes are to be returned.
- ///
- /// NO_ATTRS = "1.1"
- /// </summary>
- public const System.String NO_ATTRS = "1.1";
-
- /// <summary> Used with search instead of an attribute list to indicate that all
- /// attributes are to be returned.
- ///
- /// ALL_USER_ATTRS = "*"
- /// </summary>
- public const System.String ALL_USER_ATTRS = "*";
-
- /// <summary> Specifies the Ldapv3 protocol version when performing a bind operation.
- ///
- /// Specifies Ldap version V3 of the protocol, and is specified
- /// when performing bind operations.
- /// You can use this identifier in the version parameter
- /// of the bind method to specify an Ldapv3 bind.
- /// Ldap_V3 is the default protocol version
- ///
- /// Ldap_V3 = 3
- ///
- /// </summary>
- public const int Ldap_V3 = 3;
-
- /// <summary> The default port number for Ldap servers.
- ///
- /// You can use this identifier to specify the port when establishing
- /// a clear text connection to a server. This the default port.
- ///
- /// DEFAULT_PORT = 389
- ///
- /// </summary>
- public const int DEFAULT_PORT = 389;
-
-
- /// <summary> The default SSL port number for Ldap servers.
- ///
- /// DEFAULT_SSL_PORT = 636
- ///
- /// You can use this identifier to specify the port when establishing
- /// a an SSL connection to a server..
- /// </summary>
- public const int DEFAULT_SSL_PORT = 636;
-
- /// <summary> A string that can be passed in to the getProperty method.
- ///
- /// Ldap_PROPERTY_SDK = "version.sdk"
- ///
- /// You can use this string to request the version of the SDK.
- /// </summary>
- public const System.String Ldap_PROPERTY_SDK = "version.sdk";
-
- /// <summary> A string that can be passed in to the getProperty method.
- ///
- /// Ldap_PROPERTY_PROTOCOL = "version.protocol"
- ///
- /// You can use this string to request the version of the
- /// Ldap protocol.
- /// </summary>
- public const System.String Ldap_PROPERTY_PROTOCOL = "version.protocol";
-
- /// <summary> A string that can be passed in to the getProperty method.
- ///
- /// Ldap_PROPERTY_SECURITY = "version.security"
- ///
- /// You can use this string to request the type of security
- /// being used.
- /// </summary>
- public const System.String Ldap_PROPERTY_SECURITY = "version.security";
-
- /// <summary> A string that corresponds to the server shutdown notification OID.
- /// This notification may be used by the server to advise the client that
- /// the server is about to close the connection due to an error
- /// condition.
- ///
- /// SERVER_SHUTDOWN_OID = "1.3.6.1.4.1.1466.20036"
- /// </summary>
- public const System.String SERVER_SHUTDOWN_OID = "1.3.6.1.4.1.1466.20036";
-
- /// <summary> The OID string that identifies a StartTLS request and response.</summary>
- private const System.String START_TLS_OID = "1.3.6.1.4.1.1466.20037";
-
- public event CertificateValidationCallback UserDefinedServerCertValidationDelegate
- {
- add
- {
- this.conn.OnCertificateValidation += value;
- }
- remove
- {
- this.conn.OnCertificateValidation -= value;
- }
- }
- /*
- * Constructors
- */
-
-
- /// <summary> Constructs a new LdapConnection object, which will use the supplied
- /// class factory to construct a socket connection during
- /// LdapConnection.connect method.
- ///
- /// </summary>
- /// <param name="factory"> An object capable of producing a Socket.
- ///
- /// </param>
- public LdapConnection()
- {
- InitBlock();
- // Get a unique connection name for debug
- conn = new Connection();
- return ;
- }
-
- /* public LdapConnection(X509Certificate cert)
- {
- InitBlock();
- // Get a unique connection name for debug
- conn = new Connection();
- conn.Cert = cert;
- return ;
- }
- */
- /*
- * The following are methods that affect the operation of
- * LdapConnection, but are not Ldap requests.
- */
-
- /// <summary> Returns a copy of the object with a private context, but sharing the
- /// network connection if there is one.
- ///
- /// The network connection remains open until all clones have
- /// disconnected or gone out of scope. Any connection opened after
- /// cloning is private to the object making the connection.
- ///
- /// The clone can issue requests and freely modify options and search
- /// constraints, and , without affecting the source object or other clones.
- /// If the clone disconnects or reconnects, it is completely dissociated
- /// from the source object and other clones. Reauthenticating in a clone,
- /// however, is a global operation which will affect the source object
- /// and all associated clones, because it applies to the single shared
- /// physical connection. Any request by an associated object after one
- /// has reauthenticated will carry the new identity.
- ///
- /// </summary>
- /// <returns> A of the object.
- /// </returns>
- public System.Object Clone()
- {
- LdapConnection newClone;
- System.Object newObj;
- try
- {
- newObj = base.MemberwiseClone();
- newClone = (LdapConnection) newObj;
- }
- catch (System.Exception ce)
- {
- throw new System.SystemException("Internal error, cannot create clone");
- }
- newClone.conn = conn; // same underlying connection
-
- //now just duplicate the defSearchCons and responseCtls
- if (defSearchCons != null)
- {
- newClone.defSearchCons = (LdapSearchConstraints) defSearchCons.Clone();
- }
- else
- {
- newClone.defSearchCons = null;
- }
- if (responseCtls != null)
- {
- newClone.responseCtls = new LdapControl[responseCtls.Length];
- for (int i = 0; i < responseCtls.Length; i++)
- {
- newClone.responseCtls[i] = (LdapControl) responseCtls[i].Clone();
- }
- }
- else
- {
- newClone.responseCtls = null;
- }
- conn.incrCloneCount(); // Increment the count of clones
- return newObj;
- }
-
- /// <summary> Closes the connection, if open, and releases any other resources held
- /// by the object.
- ///
- /// </summary>
- /// <exception> LdapException A general exception which includes an error
- /// message and an Ldap error code.
- ///
- /// </exception>
- /// <seealso cref="Disconnect">
- /// </seealso>
- ~LdapConnection()
- {
- // Disconnect did not come from user API call
- Disconnect(defSearchCons, false);
- return ;
- }
-
- /// <summary> Returns a property of a connection object.
- ///
- /// </summary>
- /// <param name="name"> Name of the property to be returned.
- ///
- /// The following read-only properties are available
- /// for any given connection:
- /// <ul>
- /// <li>Ldap_PROPERTY_SDK returns the version of this SDK,
- /// as a Float data type.</li>
- ///
- /// <li>Ldap_PROPERTY_PROTOCOL returns the highest supported version of
- /// the Ldap protocol, as a Float data type.</li>
- ///
- /// <li>Ldap_PROPERTY_SECURITY returns a comma-separated list of the
- /// types of authentication supported, as a
- /// string.</li>
- /// </ul>
- ///
- /// A deep copy of the property is provided where applicable; a
- /// client does not need to clone the object received.
- ///
- /// </param>
- /// <returns> The object associated with the requested property,
- /// or null if the property is not defined.
- ///
- /// </returns>
- /// <seealso cref="LdapConstraints.getProperty">
- /// </seealso>
- /// <seealso cref="Object">
- /// </seealso>
- public virtual System.Object getProperty(System.String name)
- {
- if (name.ToUpper().Equals(Ldap_PROPERTY_SDK.ToUpper()))
- return Connection.sdk;
- else if (name.ToUpper().Equals(Ldap_PROPERTY_PROTOCOL.ToUpper()))
- return Connection.protocol;
- else if (name.ToUpper().Equals(Ldap_PROPERTY_SECURITY.ToUpper()))
- return Connection.security;
- else
- {
- return null;
- }
- }
-
- /// <summary> Registers an object to be notified on arrival of an unsolicited
- /// message from a server.
- ///
- /// An unsolicited message has the ID 0. A new thread is created and
- /// the method "messageReceived" in each registered object is called in
- /// turn.
- ///
- /// </summary>
- /// <param name="listener"> An object to be notified on arrival of an
- /// unsolicited message from a server. This object must
- /// implement the LdapUnsolicitedNotificationListener interface.
- ///
- /// </param>
- public virtual void AddUnsolicitedNotificationListener(LdapUnsolicitedNotificationListener listener)
- {
- if (listener != null)
- conn.AddUnsolicitedNotificationListener(listener);
- }
-
-
-
- /// <summary> Deregisters an object so that it will no longer be notified on
- /// arrival of an unsolicited message from a server. If the object is
- /// null or was not previously registered for unsolicited notifications,
- /// the method does nothing.
- ///
- ///
- /// </summary>
- /// <param name="listener"> An object to no longer be notified on arrival of
- /// an unsolicited message from a server.
- ///
- /// </param>
- public virtual void RemoveUnsolicitedNotificationListener(LdapUnsolicitedNotificationListener listener)
- {
-
- if (listener != null)
- conn.RemoveUnsolicitedNotificationListener(listener);
- }
-
- /// <summary> Starts Transport Layer Security (TLS) protocol on this connection
- /// to enable session privacy.
- ///
- /// This affects the LdapConnection object and all cloned objects. A
- /// socket factory that implements LdapTLSSocketFactory must be set on the
- /// connection.
- ///
- /// </summary>
- /// <exception> LdapException Thrown if TLS cannot be started. If a
- /// SocketFactory has been specified that does not implement
- /// LdapTLSSocketFactory an LdapException is thrown.
- ///
- /// </exception>
- public virtual void startTLS()
- {
-
- LdapMessage startTLS = MakeExtendedOperation(new LdapExtendedOperation(LdapConnection.START_TLS_OID, null), null);
-
- int tlsID = startTLS.MessageID;
-
- conn.acquireWriteSemaphore(tlsID);
- try
- {
- if (!conn.areMessagesComplete())
- {
- throw new LdapLocalException(ExceptionMessages.OUTSTANDING_OPERATIONS, LdapException.OPERATIONS_ERROR);
- }
- // Stop reader when response to startTLS request received
- conn.stopReaderOnReply(tlsID);
-
- // send tls message
- LdapResponseQueue queue = SendRequestToServer(startTLS, defSearchCons.TimeLimit, null, null);
-
- LdapExtendedResponse response = (LdapExtendedResponse) queue.getResponse();
- response.chkResultCode();
-
- conn.startTLS();
- }
- finally
- {
- //Free this semaphore no matter what exceptions get thrown
- conn.startReader();
- conn.freeWriteSemaphore(tlsID);
- }
- return ;
- }
-
- /// <summary> Stops Transport Layer Security(TLS) on the LDAPConnection and reverts
- /// back to an anonymous state.
- ///
- /// @throws LDAPException This can occur for the following reasons:
- /// <UL>
- /// <LI>StartTLS has not been called before stopTLS</LI>
- /// <LI>There exists outstanding messages that have not received all
- /// responses</LI>
- /// <LI>The sever was not able to support the operation</LI></UL>
- ///
- /// <p>Note: The Sun and IBM implementions of JSSE do not currently allow
- /// stopping TLS on an open Socket. In order to produce the same results
- /// this method currently disconnects the socket and reconnects, giving
- /// the application an anonymous connection to the server, as required
- /// by StopTLS</p>
- /// </summary>
- public virtual void stopTLS()
- {
-
- if (!TLS)
- {
- throw new LdapLocalException(ExceptionMessages.NO_STARTTLS, LdapException.OPERATIONS_ERROR);
- }
-
- int semaphoreID = conn.acquireWriteSemaphore();
- try
- {
- if (!conn.areMessagesComplete())
- {
- throw new LdapLocalException(ExceptionMessages.OUTSTANDING_OPERATIONS, LdapException.OPERATIONS_ERROR);
- }
- //stopTLS stops and starts the reader thread for us.
- conn.stopTLS();
- }
- finally
- {
- conn.freeWriteSemaphore(semaphoreID);
-
- /* Now that the TLS socket is closed, reset everything. This next
- line is temporary until JSSE is fixed to properly handle TLS stop */
- this.Connect(this.Host, this.Port);
- }
- return ;
- }
-
- //*************************************************************************
- // Below are all of the Ldap protocol operation methods
- //*************************************************************************
-
- //*************************************************************************
- // abandon methods
- //*************************************************************************
-
- /// <summary>
- ///
- /// Notifies the server not to send additional results associated with
- /// this LdapSearchResults object, and discards any results already
- /// received.
- ///
- /// </summary>
- /// <param name="results"> An object returned from a search.
- ///
- /// </param>
- /// <exception> LdapException A general exception which includes an error
- /// message and an Ldap error code.
- /// </exception>
- public virtual void Abandon(LdapSearchResults results)
- {
- Abandon(results, defSearchCons);
- return ;
- }
-
- /// <summary>
- ///
- /// Notifies the server not to send additional results associated with
- /// this LdapSearchResults object, and discards any results already
- /// received.
- ///
- /// </summary>
- /// <param name="results"> An object returned from a search.
- ///
- /// </param>
- /// <param name="cons"> The contraints specific to the operation.
- ///
- /// </param>
- /// <exception> LdapException A general exception which includes an error
- /// message and an Ldap error code.
- /// </exception>
- public virtual void Abandon(LdapSearchResults results, LdapConstraints cons)
- {
- results.Abandon();
- return ;
- }
-
- /// <summary>
- /// Abandons an asynchronous operation.
- ///
- /// </summary>
- /// <param name="id"> The ID of the asynchronous operation to abandon. The ID
- /// can be obtained from the response queue for the
- /// operation.
- ///
- /// </param>
- /// <exception> LdapException A general exception which includes an error
- /// message and an Ldap error code.
- /// </exception>
- public virtual void Abandon(int id)
- {
- Abandon(id, defSearchCons);
- return ;
- }
-
- /// <summary> Abandons an asynchronous operation, using the specified
- /// constraints.
- ///
- /// </summary>
- /// <param name="id">The ID of the asynchronous operation to abandon.
- /// The ID can be obtained from the search
- /// queue for the operation.
- ///
- /// </param>
- /// <param name="cons">The contraints specific to the operation.
- ///
- /// </param>
- /// <exception> LdapException A general exception which includes an error
- /// message and an Ldap error code.
- /// </exception>
- public virtual void Abandon(int id, LdapConstraints cons)
- {
- // We need to inform the Message Agent which owns this messageID to
- // remove it from the queue.
- try
- {
- MessageAgent agent = conn.getMessageAgent(id);
- agent.Abandon(id, cons);
- return ;
- }
- catch (System.FieldAccessException ex)
- {
- return ; // Ignore error
- }
- }
-
- /// <summary> Abandons all outstanding operations managed by the queue.
- ///
- /// All operations in progress, which are managed by the specified queue,
- /// are abandoned.
- ///
- /// </summary>
- /// <param name="queue"> The queue returned from an asynchronous request.
- /// All outstanding operations managed by the queue
- /// are abandoned, and the queue is emptied.
- ///
- /// </param>
- /// <exception> LdapException A general exception which includes an error
- /// message and an Ldap error code.
- /// </exception>
- public virtual void Abandon(LdapMessageQueue queue)
- {
- Abandon(queue, defSearchCons);
- return ;
- }
-
- /// <summary> Abandons all outstanding operations managed by the queue.
- ///
- /// All operations in progress, which are managed by the specified
- /// queue, are abandoned.
- ///
- /// </summary>
- /// <param name="queue"> The queue returned from an asynchronous request.
- /// All outstanding operations managed by the queue
- /// are abandoned, and the queue is emptied.
- ///
- /// </param>
- /// <param name="cons"> The contraints specific to the operation.
- ///
- /// </param>
- /// <exception> LdapException A general exception which includes an error
- /// message and an Ldap error code.
- /// </exception>
- public virtual void Abandon(LdapMessageQueue queue, LdapConstraints cons)
- {
- if (queue != null)
- {
- MessageAgent agent;
- if (queue is LdapSearchQueue)
- {
- agent = queue.MessageAgent;
- }
- else
- {
- agent = queue.MessageAgent;
- }
- int[] msgIds = agent.MessageIDs;
- for (int i = 0; i < msgIds.Length; i++)
- {
- agent.Abandon(msgIds[i], cons);
- }
- }
- return ;
- }
-
- //*************************************************************************
- // add methods
- //*************************************************************************
-
- /// <summary> Synchronously adds an entry to the directory.
- ///
- /// </summary>
- /// <param name="entry"> LdapEntry object specifying the distinguished
- /// name and attributes of the new entry.
- ///
- /// </param>
- /// <exception> LdapException A general exception which includes an error
- /// message and an Ldap error code.
- /// </exception>
- public virtual void Add(LdapEntry entry)
- {
- Add(entry, defSearchCons);
- return ;
- }
-
- /// <summary>
- /// Synchronously adds an entry to the directory, using the specified
- /// constraints.
- ///
- /// </summary>
- /// <param name="entry"> LdapEntry object specifying the distinguished
- /// name and attributes of the new entry.
- ///
- /// </param>
- /// <param name="cons"> Constraints specific to the operation.
- ///
- /// </param>
- /// <exception> LdapException A general exception which includes an error
- /// message and an Ldap error code.
- /// </exception>
-
- public virtual void Add(LdapEntry entry, LdapConstraints cons)
- {
- LdapResponseQueue queue = Add(entry, null, cons);
-
- // Get a handle to the add response
- LdapResponse addResponse = (LdapResponse) (queue.getResponse());
-
- // Set local copy of responseControls synchronously if there were any
- lock (responseCtlSemaphore)
- {
- responseCtls = addResponse.Controls;
- }
- chkResultCode(queue, cons, addResponse);
- return ;
- }
-
- /// <summary> Asynchronously adds an entry to the directory.
- ///
- /// </summary>
- /// <param name="entry"> LdapEntry object specifying the distinguished
- /// name and attributes of the new entry.
- ///
- /// </param>
- /// <param name="queue"> Handler for messages returned from a server in
- /// response to this request. If it is null, a
- /// queue object is created internally.
- ///
- /// </param>
- /// <exception> LdapException A general exception which includes an error
- /// message and an Ldap error code.
- /// </exception>
- public virtual LdapResponseQueue Add(LdapEntry entry, LdapResponseQueue queue)
- {
- return Add(entry, queue, defSearchCons);
- }
-
- /// <summary> Asynchronously adds an entry to the directory, using the specified
- /// constraints.
- ///
- /// </summary>
- /// <param name="entry"> LdapEntry object specifying the distinguished
- /// name and attributes of the new entry.
- ///
- /// </param>
- /// <param name="queue"> Handler for messages returned from a server in
- /// response to this request. If it is null, a
- /// queue object is created internally.
- ///
- /// </param>
- /// <param name="cons"> Constraints specific to the operation.
- ///
- /// </param>
- /// <exception> LdapException A general exception which includes an error
- /// message and an Ldap error code.
- /// </exception>
- public virtual LdapResponseQueue Add(LdapEntry entry, LdapResponseQueue queue, LdapConstraints cons)
- {
- if (cons == null)
- cons = defSearchCons;
-
- // error check the parameters
- if (entry == null)
- {
- throw new System.ArgumentException("The LdapEntry parameter" + " cannot be null");
- }
- if ((System.Object) entry.DN == null)
- {
- throw new System.ArgumentException("The DN value must be present" + " in the LdapEntry object");
- }
-
- LdapMessage msg = new LdapAddRequest(entry, cons.getControls());
-
- return SendRequestToServer(msg, cons.TimeLimit, queue, null);
- }
-
- //*************************************************************************
- // bind methods
- //*************************************************************************
-
- /// <summary> Synchronously authenticates to the Ldap server (that the object is
- /// currently connected to) as an Ldapv3 bind, using the specified name and
- /// password.
- ///
- /// If the object has been disconnected from an Ldap server,
- /// this method attempts to reconnect to the server. If the object
- /// has already authenticated, the old authentication is discarded.
- ///
- /// </summary>
- /// <param name="dn"> If non-null and non-empty, specifies that the
- /// connection and all operations through it should
- /// be authenticated with dn as the distinguished
- /// name.
- ///
- /// </param>
- /// <param name="passwd"> If non-null and non-empty, specifies that the
- /// connection and all operations through it should
- /// be authenticated with dn as the distinguished
- /// name and passwd as password.
- ///
- /// Note: the application should use care in the use
- /// of String password objects. These are long lived
- /// objects, and may expose a security risk, especially
- /// in objects that are serialized. The LdapConnection
- /// keeps no long lived instances of these objects.
- ///
- /// </param>
- /// <exception> LdapException A general exception which includes an error
- /// message and an Ldap error code.
- ///
- /// </exception>
- public virtual void Bind(System.String dn, System.String passwd)
- {
- Bind(dn, passwd, AuthenticationTypes.None);
- return ;
- }
-
- public virtual void Bind(System.String dn, System.String passwd, AuthenticationTypes authenticationTypes)
- {
- #if TARGET_JVM
- if (authenticationTypes != AuthenticationTypes.None &&
- authenticationTypes != AuthenticationTypes.ServerBind &&
- authenticationTypes != AuthenticationTypes.Anonymous)
- BindSecure(dn, passwd, authenticationTypes);
- else
- #endif
- Bind(Ldap_V3, dn, passwd, defSearchCons);
- return ;
- }
-
- /// <summary> Synchronously authenticates to the Ldap server (that the object is
- /// currently connected to) using the specified name, password,
- /// and Ldap version.
- ///
- /// If the object has been disconnected from an Ldap server,
- /// this method attempts to reconnect to the server. If the object
- /// has already authenticated, the old authentication is discarded.
- ///
- /// </summary>
- /// <param name="version"> The Ldap protocol version, use Ldap_V3.
- /// Ldap_V2 is not supported.
- ///
- /// </param>
- /// <param name="dn"> If non-null and non-empty, specifies that the
- /// connection and all operations through it should
- /// be authenticated with dn as the distinguished
- /// name.
- ///
- /// </param>
- /// <param name="passwd"> If non-null and non-empty, specifies that the
- /// connection and all operations through it should
- /// be authenticated with dn as the distinguished
- /// name and passwd as password.
- ///
- /// Note: the application should use care in the use
- /// of String password objects. These are long lived
- /// objects, and may expose a security risk, especially
- /// in objects that are serialized. The LdapConnection
- /// keeps no long lived instances of these objects.
- ///
- /// </param>
- /// <exception> LdapException A general exception which includes an error
- /// message and an Ldap error code.
- ///
- /// </exception>
- public virtual void Bind(int version, System.String dn, System.String passwd)
- {
- Bind(version, dn, passwd, defSearchCons);
- return ;
- }
-
- /// <summary> Synchronously authenticates to the Ldap server (that the object is
- /// currently connected to) as an Ldapv3 bind, using the specified name,
- /// password, and constraints.
- ///
- /// If the object has been disconnected from an Ldap server,
- /// this method attempts to reconnect to the server. If the object
- /// has already authenticated, the old authentication is discarded.
- ///
- /// </summary>
- /// <param name="dn"> If non-null and non-empty, specifies that the
- /// connection and all operations through it should
- /// be authenticated with dn as the distinguished
- /// name.
- ///
- /// </param>
- /// <param name="passwd"> If non-null and non-empty, specifies that the
- /// connection and all operations through it should
- /// be authenticated with dn as the distinguished
- /// name and passwd as password.
- /// Note: the application should use care in the use
- /// of String password objects. These are long lived
- /// objects, and may expose a security risk, especially
- /// in objects that are serialized. The LdapConnection
- /// keeps no long lived instances of these objects.
- ///
- /// </param>
- /// <param name="cons"> Constraints specific to the operation.
- ///
- /// </param>
- /// <exception> LdapException A general exception which includes an error
- /// message and an Ldap error code.
- ///
- /// </exception>
- public virtual void Bind(System.String dn, System.String passwd, LdapConstraints cons)
- {
- Bind(Ldap_V3, dn, passwd, cons);
- return ;
- }
-
- /// <summary> Synchronously authenticates to the Ldap server (that the object is
- /// currently connected to) using the specified name, password, Ldap version,
- /// and constraints.
- ///
- /// If the object has been disconnected from an Ldap server,
- /// this method attempts to reconnect to the server. If the object
- /// has already authenticated, the old authentication is discarded.
- ///
- /// </summary>
- /// <param name="version"> The Ldap protocol version, use Ldap_V3.
- /// Ldap_V2 is not supported.
- ///
- /// </param>
- /// <param name="dn"> If non-null and non-empty, specifies that the
- /// connection and all operations through it should
- /// be authenticated with dn as the distinguished
- /// name.
- ///
- /// </param>
- /// <param name="passwd"> If non-null and non-empty, specifies that the
- /// connection and all operations through it should
- /// be authenticated with dn as the distinguished
- /// name and passwd as password.
- ///
- /// Note: the application should use care in the use
- /// of String password objects. These are long lived
- /// objects, and may expose a security risk, especially
- /// in objects that are serialized. The LdapConnection
- /// keeps no long lived instances of these objects.
- ///
- /// </param>
- /// <param name="cons"> The constraints specific to the operation.
- ///
- /// </param>
- /// <exception> LdapException A general exception which includes an error
- /// message and an Ldap error code.
- ///
- /// </exception>
- public virtual void Bind(int version, System.String dn, System.String passwd, LdapConstraints cons)
- {
- sbyte[] pw = null;
- if ((System.Object) passwd != null)
- {
- try
- {
- System.Text.Encoding encoder = System.Text.Encoding.GetEncoding("utf-8");
- byte[] ibytes = encoder.GetBytes(passwd);
- pw=SupportClass.ToSByteArray(ibytes);
- // pw = passwd.getBytes("UTF8");
- passwd = null; // Keep no reference to String object
- }
- catch (System.IO.IOException ex)
- {
- passwd = null; // Keep no reference to String object
- throw new System.SystemException(ex.ToString());
- }
- }
- Bind(version, dn, pw, cons);
- return ;
- }
-
- /// <summary> Synchronously authenticates to the Ldap server (that the object is
- /// currently connected to) using the specified name, password,
- /// and Ldap version.
- ///
- /// If the object has been disconnected from an Ldap server,
- /// this method attempts to reconnect to the server. If the object
- /// has already authenticated, the old authentication is discarded.
- ///
- /// </summary>
- /// <param name="version"> The version of the Ldap protocol to use
- /// in the bind, use Ldap_V3. Ldap_V2 is not supported.
- ///
- /// </param>
- /// <param name="dn"> If non-null and non-empty, specifies that the
- /// connection and all operations through it should
- /// be authenticated with dn as the distinguished
- /// name.
- ///
- /// </param>
- /// <param name="passwd"> If non-null and non-empty, specifies that the
- /// connection and all operations through it should
- /// be authenticated with dn as the distinguished
- /// name and passwd as password.
- ///
- /// </param>
- /// <exception> LdapException A general exception which includes an error
- /// message and an Ldap error code.
- /// </exception>
- [CLSCompliantAttribute(false)]
- public virtual void Bind(int version, System.String dn, sbyte[] passwd)
- {
- Bind(version, dn, passwd, defSearchCons);
- return ;
- }
-
- /// <summary>
- /// Synchronously authenticates to the Ldap server (that the object is
- /// currently connected to) using the specified name, password, Ldap version,
- /// and constraints.
- ///
- /// If the object has been disconnected from an Ldap server,
- /// this method attempts to reconnect to the server. If the object
- /// has already authenticated, the old authentication is discarded.
- ///
- /// </summary>
- /// <param name="version"> The Ldap protocol version, use Ldap_V3.
- /// Ldap_V2 is not supported.
- ///
- /// </param>
- /// <param name="dn"> If non-null and non-empty, specifies that the
- /// connection and all operations through it should
- /// be authenticated with dn as the distinguished
- /// name.
- ///
- /// </param>
- /// <param name="passwd"> If non-null and non-empty, specifies that the
- /// connection and all operations through it should
- /// be authenticated with dn as the distinguished
- /// name and passwd as password.
- ///
- /// </param>
- /// <param name="cons"> The constraints specific to the operation.
- ///
- /// </param>
- /// <exception> LdapException A general exception which includes an error
- /// message and an Ldap error code.
- /// </exception>
- [CLSCompliantAttribute(false)]
- public virtual void Bind(int version, System.String dn, sbyte[] passwd, LdapConstraints cons)
- {
- LdapResponseQueue queue = Bind(version, dn, passwd, null, cons, null);
- LdapResponse res = (LdapResponse) queue.getResponse();
- if (res != null)
- {
- // Set local copy of responseControls synchronously if any
- lock (responseCtlSemaphore)
- {
- responseCtls = res.Controls;
- }
-
- chkResultCode(queue, cons, res);
- }
- return ;
- }
-
- /// <summary> Asynchronously authenticates to the Ldap server (that the object is
- /// currently connected to) using the specified name, password, Ldap
- /// version, and queue.
- ///
- /// If the object has been disconnected from an Ldap server,
- /// this method attempts to reconnect to the server. If the object
- /// has already authenticated, the old authentication is discarded.
- ///
- ///
- /// </summary>
- /// <param name="version"> The Ldap protocol version, use Ldap_V3.
- /// Ldap_V2 is not supported.
- ///
- /// </param>
- /// <param name="dn"> If non-null and non-empty, specifies that the
- /// connection and all operations through it should
- /// be authenticated with dn as the distinguished
- /// name.
- ///
- /// </param>
- /// <param name="passwd"> If non-null and non-empty, specifies that the
- /// connection and all operations through it should
- /// be authenticated with dn as the distinguished
- /// name and passwd as password.
- ///
- /// </param>
- /// <param name="queue"> Handler for messages returned from a server in
- /// response to this request. If it is null, a
- /// queue object is created internally.
- ///
- /// </param>
- /// <exception> LdapException A general exception which includes an error
- /// message and an Ldap error code.
- /// </exception>
- [CLSCompliantAttribute(false)]
- public virtual LdapResponseQueue Bind(int version, System.String dn, sbyte[] passwd, LdapResponseQueue queue)
- {
- return Bind(version, dn, passwd, queue, defSearchCons, null);
- }
-
- /// <summary> Asynchronously authenticates to the Ldap server (that the object is
- /// currently connected to) using the specified name, password, Ldap
- /// version, queue, and constraints.
- ///
- /// If the object has been disconnected from an Ldap server,
- /// this method attempts to reconnect to the server. If the object
- /// had already authenticated, the old authentication is discarded.
- ///
- /// </summary>
- /// <param name="version"> The Ldap protocol version, use Ldap_V3.
- /// Ldap_V2 is not supported.
- ///
- /// </param>
- /// <param name="dn"> If non-null and non-empty, specifies that the
- /// connection and all operations through it should
- /// be authenticated with dn as the distinguished
- /// name.
- ///
- /// </param>
- /// <param name="passwd"> If non-null and non-empty, specifies that the
- /// connection and all operations through it should
- /// be authenticated with dn as the distinguished
- /// name and passwd as password.
- ///
- /// </param>
- /// <param name="queue"> Handler for messages returned from a server in
- /// response to this request. If it is null, a
- /// queue object is created internally.
- ///
- /// </param>
- /// <param name="cons"> Constraints specific to the operation.
- ///
- /// </param>
- /// <exception> LdapException A general exception which includes an error
- /// message and an Ldap error code.
- /// </exception>
- [CLSCompliantAttribute(false)]
- public virtual LdapResponseQueue Bind(int version, System.String dn, sbyte[] passwd, LdapResponseQueue queue, LdapConstraints cons, string mech)
- {
- int msgId;
- BindProperties bindProps;
- if (cons == null)
- cons = defSearchCons;
-
- if ((System.Object) dn == null)
- {
- dn = "";
- }
- else
- {
- dn = dn.Trim();
- }
-
- if (passwd == null)
- passwd = new sbyte[]{};
-
- bool anonymous = false;
- if (pa…
Large files files are truncated, but you can click here to view the full file