support ldap v3, patch by dmose, r/sr=sspitzer 198168

git-svn-id: svn://10.0.0.236/trunk@152772 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
bienvenu%nventure.com
2004-02-14 02:10:45 +00:00
parent f77280f104
commit 0c53d7eca8
19 changed files with 208 additions and 46 deletions

View File

@@ -68,8 +68,10 @@ interface nsILDAPConnection : nsISupports
* @param aBindName DN to bind as
* @param aMessageListener Callback for DNS resolution completion
* @param aClosure private parameter (anything caller desires)
* @param aVersion LDAP version to use (currently VERSION2 or
* VERSION3)
*
* @exception NS_ERROR_ILLEGAL_VALUE null pointer passed in
* @exception NS_ERROR_ILLEGAL_VALUE null pointer or invalid version
* @exception NS_ERROR_OUT_OF_MEMORY ran out of memory
* @exception NS_ERROR_OFFLINE we are in off-line mode
* @exception NS_ERROR_FAILURE
@@ -78,7 +80,10 @@ interface nsILDAPConnection : nsISupports
void init(in string aHost, in long aPort, in boolean aSSL,
in AUTF8String aBindName,
in nsILDAPMessageListener aMessageListener,
in nsISupports aClosure);
in nsISupports aClosure, in unsigned long aVersion);
const unsigned long VERSION2 = 2;
const unsigned long VERSION3 = 3;
/**
* Get information about the last error that occured on this connection.

View File

@@ -33,6 +33,8 @@
*/
#include "nsISupports.idl"
#include "nsILDAPConnection.idl"
interface nsILDAPURL;
/**
@@ -100,4 +102,13 @@ interface nsILDAPServer : nsISupports {
* @exception NS_ERROR_NULL_POINTER NULL pointer to GET method
*/
attribute nsILDAPURL url;
/**
* protocol version to be used (see nsILDAPConnection.idl for constants)
* Defaults to 3.
*
* @exception NS_ERROR_NULL_POINTER NULL pointer passed to getter
* @exception NS_ERROR_INVALID_ARG Invalid version passed to setter
*/
attribute unsigned long protocolVersion;
};

View File

@@ -63,6 +63,7 @@ nsLDAPConnection::nsLDAPConnection()
mPendingOperations(0),
mRunnable(0),
mSSL(PR_FALSE),
mVersion(nsILDAPConnection::VERSION3),
mDNSRequest(0)
{
}
@@ -167,7 +168,7 @@ NS_IMETHODIMP
nsLDAPConnection::Init(const char *aHost, PRInt32 aPort, PRBool aSSL,
const nsACString& aBindName,
nsILDAPMessageListener *aMessageListener,
nsISupports *aClosure)
nsISupports *aClosure, PRUint32 aVersion)
{
nsCOMPtr<nsIDNSListener> selfProxy;
nsresult rv;
@@ -186,13 +187,17 @@ nsLDAPConnection::Init(const char *aHost, PRInt32 aPort, PRBool aSSL,
mClosure = aClosure;
// Save the port number for later use, once the DNS server(s) has
// resolved the host part.
// Save the port number, SSL flag, and protocol version for later
// use, once the DNS server(s) has resolved the host part.
//
mPort = aPort;
// Save the SSL flag for later use
mSSL = aSSL;
if (aVersion != nsILDAPConnection::VERSION2 &&
aVersion != nsILDAPConnection::VERSION3) {
NS_ERROR("nsLDAPConnection::Init(): illegal version");
return NS_ERROR_ILLEGAL_VALUE;
}
mVersion = aVersion;
// Save the Init listener reference, we need it when the async
// DNS resolver has finished.
@@ -901,30 +906,49 @@ nsLDAPConnection::OnLookupComplete(nsIDNSRequest *aRequest,
const int lDebug = 0;
ldap_set_option(mConnectionHandle, LDAP_OPT_DEBUG_LEVEL, &lDebug);
#endif
}
// the C SDK currently defaults to v2. if we're to use v3,
// tell it so.
//
int version;
switch (mVersion) {
case 2:
break;
case 3:
version = LDAP_VERSION3;
ldap_set_option(mConnectionHandle, LDAP_OPT_PROTOCOL_VERSION,
&version);
break;
default:
NS_ERROR("nsLDAPConnection::OnLookupComplete(): mVersion"
" invalid");
}
#ifdef MOZ_PSM
// This code sets up the current connection to use PSM for SSL
// functionality. Making this use libssldap instead for
// non-browser user shouldn't be hard.
// This code sets up the current connection to use PSM for SSL
// functionality. Making this use libssldap instead for
// non-browser user shouldn't be hard.
extern nsresult nsLDAPInstallSSL(LDAP *ld, const char *aHostName);
extern nsresult nsLDAPInstallSSL(LDAP *ld, const char *aHostName);
if (mSSL) {
if (ldap_set_option(mConnectionHandle, LDAP_OPT_SSL, LDAP_OPT_ON)
!= LDAP_SUCCESS ) {
NS_ERROR("nsLDAPConnection::OnStopLookup(): Error configuring"
" connection to use SSL");
rv = NS_ERROR_UNEXPECTED;
if (mSSL) {
if (ldap_set_option(mConnectionHandle, LDAP_OPT_SSL,
LDAP_OPT_ON) != LDAP_SUCCESS ) {
NS_ERROR("nsLDAPConnection::OnStopLookup(): Error"
" configuring connection to use SSL");
rv = NS_ERROR_UNEXPECTED;
}
rv = nsLDAPInstallSSL(mConnectionHandle, mDNSHost.get());
if (NS_FAILED(rv)) {
NS_ERROR("nsLDAPConnection::OnStopLookup(): Error"
" installing secure LDAP routines for"
" connection");
}
}
rv = nsLDAPInstallSSL(mConnectionHandle, mDNSHost.get());
if (NS_FAILED(rv)) {
NS_ERROR("nsLDAPConnection::OnStopLookup(): Error installing"
" secure LDAP routines for connection");
}
}
#endif
}
// Create a new runnable object, and increment the refcnt. The
// thread will also hold a strong ref to the runnable, but we need
// to make sure it doesn't get destructed until we are done with

View File

@@ -121,6 +121,7 @@ class nsLDAPConnection : public nsILDAPConnection,
PRInt32 mPort; // The LDAP port we're binding to
PRBool mSSL; // the options
PRUint32 mVersion; // LDAP protocol version
nsCString mResolvedIP; // Preresolved list of host IPs
nsCOMPtr<nsILDAPMessageListener> mInitListener; // Init callback

View File

@@ -18,6 +18,7 @@
* Rights Reserved.
*
* Contributor(s): Leif Hedstrom <leif@netscape.com>
* Dan Mosedale <dmose@mozilla.org>
*
* Alternatively, the contents of this file may be used under the
* terms of the GNU General Public License Version 2 or later (the
@@ -38,9 +39,9 @@
NS_IMPL_THREADSAFE_ISUPPORTS1(nsLDAPServer, nsILDAPServer)
nsLDAPServer::nsLDAPServer()
: mSizeLimit(0),
mProtocolVersion(nsILDAPConnection::VERSION3)
{
mSizeLimit = 0;
}
nsLDAPServer::~nsLDAPServer()
@@ -137,3 +138,26 @@ NS_IMETHODIMP nsLDAPServer::SetUrl(nsILDAPURL *aURL)
mURL = aURL;
return NS_OK;
}
// attribute long protocolVersion
NS_IMETHODIMP nsLDAPServer::GetProtocolVersion(PRUint32 *_retval)
{
if (!_retval) {
NS_ERROR("nsLDAPServer::GetProtocolVersion: null pointer ");
return NS_ERROR_NULL_POINTER;
}
*_retval = mProtocolVersion;
return NS_OK;
}
NS_IMETHODIMP nsLDAPServer::SetProtocolVersion(PRUint32 aVersion)
{
if (aVersion != nsILDAPConnection::VERSION2 &&
aVersion != nsILDAPConnection::VERSION3) {
NS_ERROR("nsLDAPServer::SetProtocolVersion: invalid version");
return NS_ERROR_INVALID_ARG;
}
mProtocolVersion = aVersion;
return NS_OK;
}

View File

@@ -60,7 +60,7 @@ class nsLDAPServer : public nsILDAPServer
nsCString mPassword; // Password to bind with
nsCString mBindDN; // DN associated with the UID above
PRUint32 mSizeLimit; // Limit the LDAP search to this # of entries
PRUint32 mProtocolVersion; // What version of LDAP to use?
// This "links" to a LDAP URL object, which holds further information
// related to the LDAP server. Like Host, port, base-DN and scope.
nsCOMPtr<nsILDAPURL> mURL;

View File

@@ -708,6 +708,7 @@ nsLDAPService::EstablishConnection(nsLDAPServiceEntry *aEntry,
nsCAutoString password;
PRInt32 port;
PRUint32 options;
PRUint32 protocolVersion;
nsresult rv;
server = aEntry->GetServer();
@@ -715,7 +716,7 @@ nsLDAPService::EstablishConnection(nsLDAPServiceEntry *aEntry,
return NS_ERROR_FAILURE;
}
// Get username and password from the server entry.
// Get username, password, and protocol version from the server entry.
//
rv = server->GetBinddn(binddn);
if (NS_FAILED(rv)) {
@@ -725,7 +726,11 @@ nsLDAPService::EstablishConnection(nsLDAPServiceEntry *aEntry,
if (NS_FAILED(rv)) {
return NS_ERROR_FAILURE;
}
rv = server->GetProtocolVersion(&protocolVersion);
if (NS_FAILED(rv)) {
return NS_ERROR_FAILURE;
}
// Get the host and port out of the URL, which is in the server entry.
//
rv = server->GetUrl(getter_AddRefs(url));
@@ -758,7 +763,7 @@ nsLDAPService::EstablishConnection(nsLDAPServiceEntry *aEntry,
//
rv = conn->Init(host.get(), port,
(options & nsILDAPURL::OPT_SECURE) ? PR_TRUE : PR_FALSE,
binddn, this, nsnull);
binddn, this, nsnull, protocolVersion);
if (NS_FAILED(rv)) {
switch (rv) {
// Only pass along errors we are aware of

View File

@@ -89,7 +89,7 @@ class nsLDAPServiceEntry
inline PRBool DeleteEntry();
protected:
PRUint32 mLeases; // The number of leases currently granted
PRUint32 mLeases; // The number of leases currently granted
PRTime mTimestamp; // Last time this server was "used"
PRBool mDelete; // This entry is due for deletion
PRBool mRebinding; // Keep state if we are rebinding or not

View File

@@ -1279,7 +1279,8 @@ nsLDAPMessageRDFDelegateFactory.prototype =
["@mozilla.org/network/ldap-connection;1"].
createInstance(Components.interfaces.nsILDAPConnection);
connection.init(queryURL.host, queryURL.port, null,
generateGetTargetsBoundCallback(), null)
generateGetTargetsBoundCallback(), null,
Components.interfaces.nsILDAPConnection.VERSION3);
// XXXdmose - in this case, we almost certainly shouldn't be
// falling through to an error case, but instead returning

View File

@@ -793,6 +793,19 @@ function setupLdapAutocompleteSession()
// if we don't have this pref, no big deal
}
// set the LDAP protocol version correctly
var protocolVersion;
try {
protocolVersion = sPrefs.getCharPref(autocompleteDirectory +
".protocolVersion");
} catch (ex) {
// if we don't have this pref, no big deal
}
if (protocolVersion == "2") {
LDAPSession.login =
Components.interfaces.nsILDAPConnection.VERSION2;
}
// find out if we need to authenticate, and if so, tell the LDAP
// autocomplete session how to prompt for a password. This window
// (the compose window) is being used to parent the authprompter.

View File

@@ -135,12 +135,14 @@ nsresult nsAbLDAPDirectory::InitiateConnection ()
* change.
*
* But the uri value was also the means by which third-party
* products could integrate with Mozilla's LDAP Address Books without
* necessarily having an entry in the preferences file or more importantly
* needing to be able to change the preferences entries. Thus to set the
* URI Spec now, it is only necessary to read the uri pref entry, while in the case
* where it is not a preference, we need to replace the "moz-abldapdirectory".
*/
* products could integrate with Mozilla's LDAP Address Books
* without necessarily having an entry in the preferences file
* or more importantly needing to be able to change the
* preferences entries. Thus to set the URI Spec now, it is
* only necessary to read the uri pref entry, while in the
* case where it is not a preference, we need to replace the
* "moz-abldapdirectory".
*/
nsCAutoString tempLDAPURL(mURINoQuery);
tempLDAPURL.ReplaceSubstring("moz-abldapdirectory:", "ldap:");
rv = mURL->SetSpec(tempLDAPURL);
@@ -163,6 +165,24 @@ nsresult nsAbLDAPDirectory::InitiateConnection ()
mLogin.Truncate(); // zero out mLogin
}
// get the protocol version, if there is any. using a string pref
// here instead of an int, as protocol versions sometimes have names like
// "4bis".
//
nsXPIDLCString protocolVersion;
rv = prefs->GetCharPref(
PromiseFlatCString(
Substring(mURINoQuery, kLDAPDirectoryRootLen,
mURINoQuery.Length() - kLDAPDirectoryRootLen)
+ NS_LITERAL_CSTRING(".protocolVersion")).get(),
getter_Copies(protocolVersion));
if (NS_SUCCEEDED(rv) && protocolVersion.Equals("2")) {
mProtocolVersion = nsILDAPConnection::VERSION2;
}
// otherwise we leave mProtocolVersion as the default (see the initializers
// for the nsAbLDAPDirectoryQuery class).
mConnection = do_CreateInstance(NS_LDAPCONNECTION_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);

View File

@@ -631,6 +631,7 @@ nsresult nsAbQueryLDAPMessageListener::QueryResultStatus (nsISupportsArray* prop
NS_IMPL_THREADSAFE_ISUPPORTS1(nsAbLDAPDirectoryQuery, nsIAbDirectoryQuery)
nsAbLDAPDirectoryQuery::nsAbLDAPDirectoryQuery() :
mProtocolVersion (nsILDAPConnection::VERSION3),
mInitialized(PR_FALSE),
mCounter (1),
mLock (nsnull)
@@ -825,7 +826,7 @@ NS_IMETHODIMP nsAbLDAPDirectoryQuery::DoQuery(nsIAbDirectoryQueryArguments* argu
// Now lets initialize the LDAP connection properly. We'll kick
// off the bind operation in the callback function, |OnLDAPInit()|.
rv = ldapConnection->Init(host.get(), port, options, mLogin,
messageListener, nsnull);
messageListener, nsnull, mProtocolVersion);
NS_ENSURE_SUCCESS(rv, rv);
return rv;

View File

@@ -21,6 +21,7 @@
*
* Contributor(s):
* Created by: Paul Sandoz <paul.sandoz@sun.com>
* Dan Mosedale <dmose@mozilla.org>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
@@ -74,6 +75,7 @@ protected:
nsresult Initiate ();
nsXPIDLCString mLogin; // authenticate to the LDAP server as...
nsCOMPtr<nsILDAPURL> mDirectoryUrl; // the URL for the server
PRUint32 mProtocolVersion; // version of LDAP (see nsILDAPConnection.idl)
private:
nsHashtable mListeners;

View File

@@ -164,10 +164,17 @@ NS_IMETHODIMP nsAbLDAPReplicationQuery::ConnectToLDAPServer(nsILDAPURL *aURL, co
return rv;
}
PRUint32 protocolVersion;
if (DIR_TestFlag(mDirServer, DIR_LDAP_VERSION3)) {
protocolVersion = nsILDAPConnection::VERSION3;
} else {
protocolVersion = nsILDAPConnection::VERSION2;
}
// initialize the LDAP connection
return mConnection->Init(host.get(), port,
(options & nsILDAPURL::OPT_SECURE) ? PR_TRUE : PR_FALSE,
aAuthDN, listener, nsnull);
aAuthDN, listener, nsnull, protocolVersion);
}
NS_IMETHODIMP nsAbLDAPReplicationQuery::Init(const nsACString & aPrefName, nsIWebProgressListener *aProgressListener)

View File

@@ -21,6 +21,7 @@
*
* Contributor(s):
* Seth Spitzer <sspitzer@netscape.com>
* Dan Mosedale <dmose@mozilla.org>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
@@ -1457,6 +1458,8 @@ DIR_PrefId DIR_AtomizePrefName(const char *prefname)
break;
}
break;
case 'r': /* protocolVersion */
rc = idProtocolVersion;
}
break;
@@ -2974,6 +2977,11 @@ void DIR_GetPrefsForOneServer (DIR_Server *server, PRBool reinitialize, PRBool o
if (server->savePassword)
server->password = DIR_GetStringPref (prefstring, "auth.password", tempstring, "");
char *versionString = DIR_GetStringPref(prefstring, "protocolVersion",
tempstring, "3");
DIR_ForceFlag(server, DIR_LDAP_VERSION3, !strcmp(versionString, "3"));
nsCRT::free(versionString);
prefBool = DIR_GetBoolPref (prefstring, "autoComplete.enabled", tempstring, kDefaultAutoCompleteEnabled);
DIR_ForceFlag (server, DIR_AUTO_COMPLETE_ENABLED, prefBool);
prefBool = DIR_GetBoolPref (prefstring, "autoComplete.never", tempstring, kDefaultAutoCompleteNever);
@@ -3840,6 +3848,10 @@ void DIR_SavePrefsForOneServer(DIR_Server *server)
DIR_SetBoolPref (prefstring, "vlvDisabled", tempstring, DIR_TestFlag(server, DIR_LDAP_VLV_DISABLED), kDefaultVLVDisabled);
DIR_SetStringPref(prefstring, "protocolVersion", tempstring,
DIR_TestFlag(server, DIR_LDAP_VERSION3) ? "3" : "2",
"3");
DIR_SaveCustomAttributes (prefstring, tempstring, server);
DIR_SaveCustomFilters (prefstring, tempstring, server);

View File

@@ -180,7 +180,8 @@ typedef enum
idReplExcludedAttributes,
idReplExcludedAttributesCount,
idPalmCategory,
idPalmSyncTimeStamp
idPalmSyncTimeStamp,
idProtocolVersion
} DIR_PrefId;
@@ -403,7 +404,7 @@ PRBool DIR_SetServerStringPref(DIR_Server *server, DIR_PrefId prefid, char *
#define DIR_IS_SECURE 0x00000008
#define DIR_SAVE_RESULTS 0x00000010 /* not used by the FEs */
#define DIR_EFFICIENT_WILDCARDS 0x00000020 /* not used by the FEs */
#define DIR_LDAP_VERSION3 0x00000040 /* not used by the FEs */
#define DIR_LDAP_VERSION3 0x00000040
#define DIR_LDAP_VLV_DISABLED 0x00000080 /* not used by the FEs */
#define DIR_LDAP_VLV_SUPPORTED 0x00000100 /* not used by the FEs */
#define DIR_LDAP_ROOTDSE_PARSED 0x00000200 /* not used by the FEs */

View File

@@ -113,4 +113,13 @@ interface nsILDAPAutoCompleteSession : nsIAutoCompleteSession {
* If set, use this object to get a password for logging in to the server.
*/
attribute nsIAuthPrompt authPrompter;
/**
* What version of the LDAP protocol should be used? Allowed version
* number constants are defined in nsILDAPConnection.idl.
*
* @exception NS_ERROR_ILLEGAL_VALUE illegal version num passed to setter
* @exception NS_ERROR_NULL_POINTER null pointer passed to getter
*/
attribute unsigned long version;
};

View File

@@ -67,7 +67,7 @@ nsLDAPAutoCompleteSession::nsLDAPAutoCompleteSession() :
mState(UNBOUND),
mFilterTemplate("(|(cn=%v1*%v2-*)(mail=%v1*%v2-*)(sn=%v1*%v2-*))"),
mMaxHits(100), mMinStringLength(2), mCjkMinStringLength(0),
mSearchAttrs(0), mSearchAttrsSize(0)
mSearchAttrs(0), mSearchAttrsSize(0), mVersion(nsILDAPConnection::VERSION3)
{
}
@@ -860,7 +860,7 @@ nsLDAPAutoCompleteSession::StartLDAPSearch()
//
rv = NS_GetProxyForObject(NS_UI_THREAD_EVENTQ,
NS_GET_IID(nsILDAPMessageListener),
NS_STATIC_CAST(nsILDAPMessageListener *, this),
NS_STATIC_CAST(nsILDAPMessageListener *, this),
PROXY_ASYNC | PROXY_ALWAYS,
getter_AddRefs(selfProxy));
if (NS_FAILED(rv)) {
@@ -1141,7 +1141,7 @@ nsLDAPAutoCompleteSession::InitConnection()
//
rv = mConnection->Init(host.get(), port,
(options & nsILDAPURL::OPT_SECURE) ? PR_TRUE
: PR_FALSE, mLogin, selfProxy, nsnull);
: PR_FALSE, mLogin, selfProxy, nsnull, mVersion);
if NS_FAILED(rv) {
switch (rv) {
@@ -1546,4 +1546,28 @@ nsLDAPAutoCompleteSession::SetAuthPrompter(nsIAuthPrompt *aAuthPrompter)
mAuthPrompter = aAuthPrompter;
return NS_OK;
}
// attribute unsigned long version;
NS_IMETHODIMP
nsLDAPAutoCompleteSession::GetVersion(PRUint32 *aVersion)
{
if (!aVersion) {
return NS_ERROR_NULL_POINTER;
}
*aVersion = mVersion;
return NS_OK;
}
NS_IMETHODIMP
nsLDAPAutoCompleteSession::SetVersion(PRUint32 aVersion)
{
if ( mVersion != nsILDAPConnection::VERSION2 &&
mVersion != nsILDAPConnection::VERSION3) {
return NS_ERROR_ILLEGAL_VALUE;
}
mVersion = aVersion;
return NS_OK;
}
#endif

View File

@@ -76,6 +76,8 @@ class nsLDAPAutoCompleteSession : public nsILDAPMessageListener,
char **mSearchAttrs; // outputFormat search attrs for SearchExt call
PRUint32 mSearchAttrsSize; // size of above array
nsCOMPtr<nsIAuthPrompt> mAuthPrompter; // used to prompt for the password
PRUint32 mVersion; // version of LDAP to use
// XXX hack until nsUTF8String exists
#define nsUTF8String nsCString
nsUTF8String mLogin; // authenticate as this user