diff --git a/mozilla/directory/xpcom/base/public/nsILDAPURL.idl b/mozilla/directory/xpcom/base/public/nsILDAPURL.idl index b84c141add4..88acab861a5 100644 --- a/mozilla/directory/xpcom/base/public/nsILDAPURL.idl +++ b/mozilla/directory/xpcom/base/public/nsILDAPURL.idl @@ -18,6 +18,7 @@ * Rights Reserved. * * Contributor(s): Dan Mosedale + * Leif Hedstrom * * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License Version 2 or later (the @@ -40,28 +41,27 @@ interface nsILDAPURL : nsIURI { /** * the distinguished name of the URL (ie the base DN for the search) * - * XXXdmose NS_ERROR_NOT_INITIALIZED may become unneccessary once - * it becomes possible to build up the URL by using the setters for its - * constituent parts - * * for the getter: * - * @exception NS_ERROR_ILLEGAL_VALUE null pointer passed in - * @exception NS_ERROR_NOT_INITIALIZED URL spec not set - * @exception NS_ERROR_OUT_OF_MEMORY ran out of memory + * @exception NS_ERROR_NULL_POINTER NULL pointer to GET method + * @exception NS_ERROR_OUT_OF_MEMORY ran out of memory */ attribute string dn; /** * the LDAP attributes to return. defaults to all attributes. + * This is currently not implemented, and the setter is also + * missing. * - * XXXdmose - there should be a setter for this too */ void getAttributes(out unsigned long count, [retval, array, size_is(count)] out string values); /** - * the scope of the search. defaults to NS_LDAP_SCOPE_BASE. + * the scope of the search. defaults to SCOPE_BASE. + * + * @exception NS_ERROR_NULL_POINTER NULL pointer to GET method + * @exception NS_ERROR_MALFORMED_URI Illegal base to SET method */ attribute long scope; @@ -87,12 +87,15 @@ interface nsILDAPURL : nsIURI { /** * any options defined for this URL (check options using a bitwise and) + * + * @exception NS_ERROR_NULL_POINTER NULL pointer to GET method + * @exception NS_ERROR_OUT_OF_MEMORY ran out of memory */ attribute unsigned long options; /** - * if this is set, this is an ldaps: URL, not an ldap: URL + * if this is set/true, this is an ldaps: URL, not an ldap: URL */ - const unsigned long NS_LDAP_URL_OPT_SECURE = 0x01; + const unsigned long OPT_SECURE = 0x01; }; diff --git a/mozilla/directory/xpcom/base/src/nsLDAPURL.cpp b/mozilla/directory/xpcom/base/src/nsLDAPURL.cpp index 38daebe4dca..2e43d4c9106 100644 --- a/mozilla/directory/xpcom/base/src/nsLDAPURL.cpp +++ b/mozilla/directory/xpcom/base/src/nsLDAPURL.cpp @@ -18,6 +18,7 @@ * Rights Reserved. * * Contributor(s): Dan Mosedale + * Leif Hedstrom * * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License Version 2 or later (the @@ -34,20 +35,28 @@ #include "nsLDAPURL.h" +// The two schemes we support, LDAP and LDAPS +// +static const char *kLDAPScheme = "ldap"; +static const char *kLDAPSSLScheme = "ldaps"; + + +// Constructor and destructor +// NS_IMPL_THREADSAFE_ISUPPORTS2(nsLDAPURL, nsILDAPURL, nsIURI) nsLDAPURL::nsLDAPURL() + : mPort(0), + mScope(SCOPE_BASE), + mOptions(0) { NS_INIT_ISUPPORTS(); } nsLDAPURL::~nsLDAPURL() { - ldap_free_urldesc(mDesc); } -static const char *kEmptyString = ""; - // A string representation of the URI. Setting the spec // causes the new spec to be parsed, initializing the URI. Setting // the spec (or any of the accessors) causes also any currently @@ -56,98 +65,147 @@ static const char *kEmptyString = ""; // attribute string spec; // NS_IMETHODIMP -nsLDAPURL::GetSpec(char* *aSpec) +nsLDAPURL::GetSpec(char **_retval) { - NS_ENSURE_ARG_POINTER(aSpec); + nsCAutoString spec; + + if (!_retval) { + NS_ERROR("nsLDAPURL::GetSpec: null pointer "); + return NS_ERROR_NULL_POINTER; + } - // copy it out - // - *aSpec = mSpec.ToNewCString(); + spec = ((mOptions & OPT_SECURE) ? kLDAPSSLScheme : kLDAPScheme); + spec.Append("://"); + if (mHost.Length() > 0) { + spec.Append(mHost); + } + if (mPort > 0) { + spec.Append(":"); + spec.AppendInt(mPort); + } + spec.Append("/"); + if (mDN.Length() > 0) { + spec.Append(mDN); + } - // XXXdmose - presumably this is the right thing. nsString.h doesn't - // document whether there can be a 0 return code. - // - if ( ! *aSpec ) { + // XXXleif: Here we need to add attributes... + // This is bug: 70611 + if (0) { + // Code to handle attributes + } + + if ((mScope) || mFilter.Length()) { + spec.Append("??"); // XXXleif: This needs to be changed with bug 70611 + if (mScope) { + if (mScope == SCOPE_ONELEVEL) { + spec.Append("one"); + } else if (mScope == SCOPE_SUBTREE) { + spec.Append("sub"); + } + } + if (mFilter.Length()) { + spec.Append("?"); + spec.Append(mFilter); + } + } + + *_retval = spec.ToNewCString(); + if (!*_retval) { return NS_ERROR_OUT_OF_MEMORY; } return NS_OK; } NS_IMETHODIMP -nsLDAPURL::SetSpec(const char * aSpec) +nsLDAPURL::SetSpec(const char *aSpec) { PRUint32 rc; + LDAPURLDesc *desc; - // save this off - // - mSpec = aSpec; - - rc = ldap_url_parse(aSpec, &mDesc); + // This is from the LDAP C-SDK, which currently doesn't + // support everything from RFC 2255... :( + rc = ldap_url_parse(aSpec, &desc); switch (rc) { case LDAP_SUCCESS: + mHost = desc->lud_host; + mPort = desc->lud_port; + mDN = desc->lud_dn; + // XXXLeif: Need code for lud_attrs here, see 70611 + mScope = desc->lud_scope; + mFilter = desc->lud_filter; + mOptions = desc->lud_options; + + ldap_free_urldesc(desc); return NS_OK; - break; case LDAP_URL_ERR_NOTLDAP: case LDAP_URL_ERR_NODN: case LDAP_URL_ERR_BADSCOPE: return NS_ERROR_MALFORMED_URI; - break; case LDAP_URL_ERR_MEM: return NS_ERROR_OUT_OF_MEMORY; - break; case LDAP_URL_ERR_PARAM: return NS_ERROR_INVALID_POINTER; - break; } - // this shouldn't happen - // + // This shouldn't happen... return NS_ERROR_UNEXPECTED; } // attribute string prePath; // -NS_IMETHODIMP nsLDAPURL::GetPrePath(char * *aPrePath) +NS_IMETHODIMP nsLDAPURL::GetPrePath(char **_retval) { return NS_ERROR_NOT_IMPLEMENTED; } -NS_IMETHODIMP nsLDAPURL::SetPrePath(const char * aPrePath) +NS_IMETHODIMP nsLDAPURL::SetPrePath(const char *aPrePath) { return NS_ERROR_NOT_IMPLEMENTED; } // attribute string scheme; // -NS_IMETHODIMP nsLDAPURL::GetScheme(char * *aScheme) +NS_IMETHODIMP nsLDAPURL::GetScheme(char **_retval) { - // need to deal with ldaps here too? - // - char *scheme = nsCRT::strdup("ldap"); - if (!scheme) { + if (!_retval) { + NS_ERROR("nsLDAPURL::GetScheme: null pointer "); + return NS_ERROR_NULL_POINTER; + } + + + *_retval = nsCRT::strdup((mOptions & OPT_SECURE) ? kLDAPSSLScheme : + kLDAPScheme); + if (!*_retval) { return NS_ERROR_OUT_OF_MEMORY; } - *aScheme = scheme; return NS_OK; } -NS_IMETHODIMP nsLDAPURL::SetScheme(const char * aScheme) +NS_IMETHODIMP nsLDAPURL::SetScheme(const char *aScheme) { - return NS_ERROR_NOT_IMPLEMENTED; + if (nsCRT::strcasecmp(aScheme, kLDAPScheme) == 0) { + mOptions ^= OPT_SECURE; + } else if (nsCRT::strcasecmp(aScheme, kLDAPSSLScheme) == 0) { + mOptions |= OPT_SECURE; + } else { + return NS_ERROR_MALFORMED_URI; + } + + return NS_OK; } // attribute string preHost; // NS_IMETHODIMP -nsLDAPURL::GetPreHost(char * *aPreHost) +nsLDAPURL::GetPreHost(char **_retval) { return NS_ERROR_NOT_IMPLEMENTED; } NS_IMETHODIMP -nsLDAPURL::SetPreHost(const char * aPreHost) +nsLDAPURL::SetPreHost(const char *aPreHost) { return NS_ERROR_NOT_IMPLEMENTED; } @@ -155,12 +213,12 @@ nsLDAPURL::SetPreHost(const char * aPreHost) // attribute string username // NS_IMETHODIMP -nsLDAPURL::GetUsername(char * *aUsername) +nsLDAPURL::GetUsername(char **_retval) { return NS_ERROR_NOT_IMPLEMENTED; } NS_IMETHODIMP -nsLDAPURL::SetUsername(const char * aUsername) +nsLDAPURL::SetUsername(const char *aUsername) { return NS_ERROR_NOT_IMPLEMENTED; } @@ -168,12 +226,12 @@ nsLDAPURL::SetUsername(const char * aUsername) // attribute string password; // NS_IMETHODIMP -nsLDAPURL::GetPassword(char * *aPassword) +nsLDAPURL::GetPassword(char **_retval) { return NS_ERROR_NOT_IMPLEMENTED; } NS_IMETHODIMP -nsLDAPURL::SetPassword(const char * aPassword) +nsLDAPURL::SetPassword(const char *aPassword) { return NS_ERROR_NOT_IMPLEMENTED; } @@ -181,60 +239,82 @@ nsLDAPURL::SetPassword(const char * aPassword) // attribute string host; // NS_IMETHODIMP -nsLDAPURL::GetHost(char * *aHost) +nsLDAPURL::GetHost(char **_retval) { - NS_ENSURE_TRUE(mDesc, NS_ERROR_NOT_INITIALIZED); - NS_ENSURE_ARG_POINTER(aHost); - - if ( ! mDesc->lud_host ) { - *aHost = nsCRT::strdup(kEmptyString); - } else { - *aHost = nsCRT::strdup(mDesc->lud_host); + if (!_retval) { + NS_ERROR("nsLDAPURL::GetHost: null pointer "); + return NS_ERROR_NULL_POINTER; } - - if ( ! *aHost ) { + + *_retval = mHost.ToNewCString(); + if (!*_retval) { return NS_ERROR_OUT_OF_MEMORY; } return NS_OK; } NS_IMETHODIMP -nsLDAPURL::SetHost(const char * aHost) +nsLDAPURL::SetHost(const char *aHost) { - return NS_ERROR_NOT_IMPLEMENTED; + mHost = aHost; + return NS_OK; } +// C-SDK URL parser defaults port 389 as "0", while nsIURI +// specifies the default to be "-1", hence the translations. +// // attribute long port; // NS_IMETHODIMP -nsLDAPURL::GetPort(PRInt32 *aPort) +nsLDAPURL::GetPort(PRInt32 *_retval) { - NS_ENSURE_ARG_POINTER(aPort); - NS_ENSURE_TRUE(mDesc, NS_ERROR_NOT_INITIALIZED); - - if ( mDesc->lud_port ) { - *aPort = mDesc->lud_port; - } else { - *aPort = -1; + if (!_retval) { + NS_ERROR("nsLDAPURL::GetPort: null pointer "); + return NS_ERROR_NULL_POINTER; } + if (!mPort) { + *_retval = -1; + } else { + *_retval = mPort; + } + return NS_OK; } NS_IMETHODIMP nsLDAPURL::SetPort(PRInt32 aPort) { - return NS_ERROR_NOT_IMPLEMENTED; + if (aPort == -1) { + mPort = 0; + } else if (aPort >= 0) { + mPort = aPort; + } else { + return NS_ERROR_MALFORMED_URI; + } + + return NS_OK; } // attribute string path; -// -NS_IMETHODIMP nsLDAPURL::GetPath(char * *aPath) +// XXXleif: For now, these are identical to SetDn()/GetDn(). +NS_IMETHODIMP nsLDAPURL::GetPath(char **_retval) { - return NS_ERROR_NOT_IMPLEMENTED; + if (!_retval) { + NS_ERROR("nsLDAPURL::GetPath: null pointer "); + return NS_ERROR_NULL_POINTER; + } + + *_retval = mDN.ToNewCString(); + if (!*_retval) { + return NS_ERROR_OUT_OF_MEMORY; + } + + return NS_OK; } -NS_IMETHODIMP nsLDAPURL::SetPath(const char * aPath) +NS_IMETHODIMP nsLDAPURL::SetPath(const char *aPath) { - return NS_ERROR_NOT_IMPLEMENTED; + mDN = aPath; + return NS_OK; } // boolean equals (in nsIURI other) @@ -267,40 +347,34 @@ NS_IMETHODIMP nsLDAPURL::Clone(nsIURI **_retval) // string resolve (in string relativePath); // -NS_IMETHODIMP nsLDAPURL::Resolve(const char *relativePath, char **_retval) +NS_IMETHODIMP nsLDAPURL::Resolve(const char *relativePath, + char **_retval) { return NS_ERROR_NOT_IMPLEMENTED; } -// the following attributes come from nsILDAPURL +// The following attributes come from nsILDAPURL // attribute string dn; // -NS_IMETHODIMP nsLDAPURL::GetDn(char * *aDn) +NS_IMETHODIMP nsLDAPURL::GetDn(char **_retval) { - if (!aDn) { - return NS_ERROR_ILLEGAL_VALUE; + if (!_retval) { + NS_ERROR("nsLDAPURL::GetDn: null pointer "); + return NS_ERROR_NULL_POINTER; } - if (!mDesc) { - return NS_ERROR_NOT_INITIALIZED; - } - - if ( mDesc->lud_dn == 0 ) { - *aDn = nsCRT::strdup(kEmptyString); - } else { - *aDn = nsCRT::strdup(mDesc->lud_dn); - } - - if ( ! *aDn ) { + *_retval = mDN.ToNewCString(); + if (!*_retval) { return NS_ERROR_OUT_OF_MEMORY; } return NS_OK; } -NS_IMETHODIMP nsLDAPURL::SetDn(const char * aDn) +NS_IMETHODIMP nsLDAPURL::SetDn(const char *aDn) { - return NS_ERROR_NOT_IMPLEMENTED; + mDN = aDn; + return NS_OK; } // void getAttributes (out unsigned long count, @@ -313,53 +387,66 @@ NS_IMETHODIMP nsLDAPURL::GetAttributes(PRUint32 *count, char ***values) // attribute long scope; // -NS_IMETHODIMP nsLDAPURL::GetScope(PRInt32 *aScope) +NS_IMETHODIMP nsLDAPURL::GetScope(PRInt32 *_retval) { - NS_ENSURE_ARG_POINTER(aScope); - NS_ENSURE_TRUE(mDesc, NS_ERROR_NOT_INITIALIZED); - - *aScope = mDesc->lud_scope; + if (!_retval) { + NS_ERROR("nsLDAPURL::GetScope: null pointer "); + return NS_ERROR_NULL_POINTER; + } + *_retval = mScope; return NS_OK; } NS_IMETHODIMP nsLDAPURL::SetScope(PRInt32 aScope) { - return NS_ERROR_NOT_IMPLEMENTED; + // Only allow scopes supported by the C-SDK + if ((aScope != SCOPE_BASE) && + (aScope != SCOPE_ONELEVEL) && + (aScope != SCOPE_SUBTREE)) { + return NS_ERROR_MALFORMED_URI; + } + + mScope = aScope; + + return NS_OK; } // attribute string filter; // -NS_IMETHODIMP nsLDAPURL::GetFilter(char * *aFilter) +NS_IMETHODIMP nsLDAPURL::GetFilter(char **_retval) { - NS_ENSURE_ARG_POINTER(aFilter); - NS_ENSURE_TRUE(mDesc, NS_ERROR_NOT_INITIALIZED); - - // if these aren't set, insert the defaults. - // - if ( ! (mDesc->lud_filter && mDesc->lud_filter[0] ) ) { - *aFilter = nsCRT::strdup("objectClass=*"); - } else { - *aFilter = nsCRT::strdup(mDesc->lud_filter); + if (!_retval) { + NS_ERROR("nsLDAPURL::GetFilter: null pointer "); + return NS_ERROR_NULL_POINTER; } - if ( ! *aFilter ) { + *_retval = mFilter.ToNewCString(); + if (!*_retval) { return NS_ERROR_OUT_OF_MEMORY; } - + return NS_OK; } -NS_IMETHODIMP nsLDAPURL::SetFilter(const char * aFilter) +NS_IMETHODIMP nsLDAPURL::SetFilter(const char *aFilter) { - return NS_ERROR_NOT_IMPLEMENTED; + mFilter = aFilter; + return NS_OK; } // attribute unsigned long options; // -NS_IMETHODIMP nsLDAPURL::GetOptions(PRUint32 *aOptions) +NS_IMETHODIMP nsLDAPURL::GetOptions(PRUint32 *_retval) { - return NS_ERROR_NOT_IMPLEMENTED; + if (!_retval) { + NS_ERROR("nsLDAPURL::GetOptions: null pointer "); + return NS_ERROR_NULL_POINTER; + } + + *_retval = mOptions; + return NS_OK; } NS_IMETHODIMP nsLDAPURL::SetOptions(PRUint32 aOptions) { - return NS_ERROR_NOT_IMPLEMENTED; + mOptions = aOptions; + return NS_OK; } diff --git a/mozilla/directory/xpcom/base/src/nsLDAPURL.h b/mozilla/directory/xpcom/base/src/nsLDAPURL.h index f8383610caf..e8b7260a204 100644 --- a/mozilla/directory/xpcom/base/src/nsLDAPURL.h +++ b/mozilla/directory/xpcom/base/src/nsLDAPURL.h @@ -18,6 +18,7 @@ * Rights Reserved. * * Contributor(s): Dan Mosedale + * Leif Hedstrom * * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License Version 2 or later (the @@ -34,8 +35,8 @@ #include "nsLDAP.h" #include "ldap.h" -#include "nsILDAPURL.h" #include "nsString.h" +#include "nsILDAPURL.h" // cb7c67f8-0053-4072-89e9-501cbd1b35ab #define NS_LDAPURL_CID \ @@ -53,6 +54,13 @@ class nsLDAPURL : public nsILDAPURL virtual ~nsLDAPURL(); protected: - LDAPURLDesc *mDesc; // the URL descriptor we're wrapping - nsCString mSpec; // string representation + nsCString mHost; // Host name of this Directory server + PRInt32 mPort; // LDAP port number + nsCString mDN; // Base Distinguished Name (Base DN) + + // Need array for attrs here... See bug: 70611. + + PRInt32 mScope; // Search scope (base, one or sub) + nsCString mFilter; // LDAP search filter + PRUint32 mOptions; // Options };