/*
* Copyright 2001-2003 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998-1999 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
*/
/*
* Copyright (c) 1996 Regents of the University of Michigan.
* All rights reserved.
*
*/
/* LIBLDAP url.c -- LDAP URL related routines
*
* LDAP URLs look like this:
* l d a p : / / hostport / dn [ ? attributes [ ? scope [ ? filter ] ] ]
*
* where:
* attributes is a comma separated list
* scope is one of these three strings: base one sub (default=base)
* filter is an string-represented filter as in RFC 1558
*
* e.g., ldap://ldap.itd.umich.edu/c=US?o,description?one?o=umich
*
* We also tolerate URLs that look like: <ldapurl> and <URL:ldapurl>
*/
#if 0
#ifndef lint
static char copyright[] = "@(#) Copyright (c) 1996 Regents of the University of Michigan.\nAll rights reserved.\n";
#endif
#endif
#include "ldap-int.h"
int
{
}
static int
{
/*
* return non-zero if this looks like a LDAP URL; zero if not
* if non-zero returned, *urlp will be moved past "ldap://" part of URL
* The data that *urlp points to is not changed by this function.
*/
return( 0 );
}
/* skip leading '<' (if any) */
if ( **urlp == '<' ) {
*enclosedp = 1;
++*urlp;
} else {
*enclosedp = 0;
}
/* skip leading "URL:" (if any) */
}
/* check for an "ldap://" prefix */
LDAP_URL_PREFIX, LDAP_URL_PREFIX_LEN ) == 0 ) {
/* skip over URL prefix and return success */
*urlp += LDAP_URL_PREFIX_LEN;
*securep = 0;
return( 1 );
}
/* check for an "ldaps://" prefix */
LDAPS_URL_PREFIX, LDAPS_URL_PREFIX_LEN ) == 0 ) {
/* skip over URL prefix and return success */
*urlp += LDAPS_URL_PREFIX_LEN;
*securep = 1;
return( 1 );
}
return( 0 ); /* not an LDAP URL */
}
int
{
/*
* Pick apart the pieces of an LDAP URL.
*/
int rc;
}
}
}
}
return( rc );
}
/* same as ldap_url_parse(), but dn is not require */
int
{
/*
* Pick apart the pieces of an LDAP URL.
*/
int rc;
}
}
}
}
return (rc);
}
/*
* like ldap_url_parse() with a few exceptions:
* 1) if dn_required is zero, a missing DN does not generate an error
* (we just leave the lud_dn field NULL)
* 2) no defaults are set for lud_scope and lud_filter (they are set to -1
* and NULL respectively if no SCOPE or FILTER are present in the URL).
* 3) when there is a zero-length DN in a URL we do not set lud_dn to NULL.
* 4) if an LDAPv3 URL extensions are included,
*/
int
{
return( LDAP_URL_ERR_PARAM );
}
return( LDAP_URL_ERR_NOTLDAP );
}
/* allocate return struct */
== NULLLDAPURLDESC ) {
return( LDAP_URL_ERR_MEM );
}
if ( secure ) {
}
/* make working copy of the remainder of the URL */
return( LDAP_URL_ERR_MEM );
}
*p = '\0';
}
/* initialize scope and filter */
/* lud_string is the only malloc'd string space we use */
/* scan forward for '/' that marks end of hostport and begin. of dn */
if ( dn_required ) {
return( LDAP_URL_ERR_NODN );
}
} else {
/* terminate hostport; point to start of dn */
}
if ( *urlcopy == '\0' ) {
} else {
/*
* Locate and strip off optional port number (:#) in host
* portion of URL.
*
* If more than one space-separated host is listed, we only
* look for a port number within the right-most one since
* ldap_init() will handle host parameters that look like
* host:port anyway.
*/
} else {
++p;
}
/* square brackets present -- skip past them */
p = q++;
}
*p++ = '\0';
/*
* no hostname and a port: invalid hostcode
* according to RFC 1738
*/
return (LDAP_URL_ERR_HOSTPORT);
}
}
}
/* scan for '?' that marks end of dn and beginning of attributes */
/* terminate dn; point to start of attrs. */
*attrs++ = '\0';
/* scan for '?' that marks end of attrs and begin. of scope */
/*
* terminate attrs; point to start of scope and scan for
* '?' that marks end of scope and begin. of filter
*/
*p++ = '\0';
scope = p;
/* terminate scope; point to start of filter */
*p++ = '\0';
if ( *p != '\0' ) {
ludp->lud_filter = p;
/*
* scan for the '?' that marks the end
* of the filter and the start of any
* extensions
*/
!= NULL ) {
*p++ = '\0'; /* term. filter */
extensions = p;
}
} else {
}
}
}
} else if ( *scope != '\0' ) {
return( LDAP_URL_ERR_BADSCOPE );
}
}
}
}
/*
* if attrs list was included, turn it into a null-terminated array
*/
if ( *p == ',' ) {
++nattrs;
}
}
sizeof( char * ))) == NULL ) {
return( LDAP_URL_ERR_MEM );
}
*p++ ='\0';
}
}
}
/* if extensions list was included, check for critical ones */
/* Note: at present, we do not recognize ANY extensions */
at_start = 1;
for ( p = extensions; *p != '\0'; ++p ) {
if ( at_start ) {
if ( *p == '!' ) { /* critical extension */
/* this is what iplanet did *
return( LDAP_URL_UNRECOGNIZED_CRITICAL_EXTENSION );
* and this is what we do */
return( LDAP_URL_ERR_PARAM );
}
at_start = 0;
} else if ( *p == ',' ) {
at_start = 1;
}
}
}
return( 0 );
}
void
{
if ( ludp != NULLLDAPURLDESC ) {
}
}
NSLDAPI_FREE( ludp );
}
}
int
{
char *host;
if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
return( -1 ); /* punt */
}
return( -1 );
}
return( -1 );
}
err = 0;
} else {
}
NSLDAPI_FREE( srv );
}
err = -1;
} else {
/* URL includes a port - use it */
/* URL has no port or host - use port from ld */
/* ldap URL has a host but no port - use std. port */
} else {
/* ldaps URL has a host but no port - use std. port */
}
}
}
if ( err != 0 ) {
} else {
}
return( err );
}
int
{
int msgid;
/*
* It is an error to pass in a zero'd timeval.
*/
}
}
return( LDAP_PARAM_ERROR );
}
}
}
return( LDAP_TIMEOUT );
}
}
int
{
int msgid;
}
}
}
#ifdef _SOLARIS_SDK
/*
* Locate the LDAP URL associated with a DNS domain name.
*
* The supplied DNS domain name is converted into a distinguished
* name. The directory entry specified by that distinguished name
* is searched for a labeledURI attribute. If successful then the
* LDAP URL is returned. If unsuccessful then that entry's parent
* is searched and so on until the target distinguished name is
* reduced to only two nameparts.
*
* For example, if 'ny.eng.wiz.com' is the DNS domain then the
* following entries are searched until one succeeds:
* dc=ny,dc=eng,dc=wiz,dc=com
* dc=eng,dc=wiz,dc=com
* dc=wiz,dc=com
*
* If dns_name is NULL then the environment variable LOCALDOMAIN is used.
* If attrs is not NULL then it is appended to the URL's attribute list.
* If scope is not NULL then it overrides the URL's scope.
* If filter is not NULL then it is merged with the URL's filter.
*
* If an error is encountered then zero is returned, otherwise a string
* URL is returned. The caller should free the returned string if it is
* non-zero.
*/
char *
char *dns_name,
char *attrs,
char *scope,
char *filter
)
{
char *dn;
char *url = 0;
char *url2 = 0;
char *cp;
char *cp2;
int nameparts;
int no_attrs = 0;
int no_scope = 0;
if (dns_name == 0) {
}
NULL))
return (0);
return (0);
}
if (attrs)
if (scope)
if (filter)
/* for ampersand, parentheses and NULL */
if (ldap_is_ldap_url(url)) {
return (0);
}
/* copy URL scheme, hostname, port number and DN */
}
/* handle URL attributes */
/* insert supplied attributes */
if (attrs) {
while (*attrs) {
}
} else {
no_attrs = 1;
}
} else {
/* copy URL attributes */
}
/* append supplied attributes */
if (attrs) {
*cp2++ = ',';
while (*attrs) {
}
}
}
} else {
/* append supplied attributes */
if (attrs) {
*cp2++ = '?';
while (*attrs) {
}
} else {
no_attrs = 1;
}
}
/* handle URL scope */
/* insert supplied scope */
if (scope) {
while (*scope) {
}
} else {
no_scope = 1;
}
} else {
if (scope) {
/* skip over URL scope */
*cp++;
}
/* insert supplied scope */
while (*scope) {
}
} else {
/* copy URL scope */
}
}
}
} else {
/* append supplied scope */
if (scope) {
if (no_attrs) {
*cp2++ = '?';
}
*cp2++ = '?';
while (*scope) {
}
} else {
no_scope = 1;
}
}
/* handle URL filter */
if (filter) {
/* merge URL and supplied filters */
*cp2++ = '(';
*cp2++ = '&';
/* copy URL filter */
while (*cp) {
}
/* append supplied filter */
while (*filter) {
}
*cp2++ = ')';
} else {
/* copy URL filter */
while (*cp) {
}
}
} else {
/* append supplied filter */
if (filter) {
if (no_scope) {
if (no_attrs) {
*cp2++ = '?';
}
*cp2++ = '?';
}
*cp2++ = '?';
while (*filter) {
}
}
}
*cp2++ = '\0';
} else {
return (0); /* not an LDAP URL */
}
}
return (url);
}
/*
* Locate the LDAP URL associated with a distinguished name.
*
* The number of nameparts in the supplied distinguished name must be
* provided. The specified directory entry is searched for a labeledURI
* attribute. If successful then the LDAP URL is returned. If unsuccessful
* then that entry's parent is searched and so on until the target
* distinguished name is reduced to only two nameparts.
*
* For example, if 'l=ny,ou=eng,o=wiz,c=us' is the distinguished name
* then the following entries are searched until one succeeds:
* l=ny,ou=eng,o=wiz,c=us
* ou=eng,o=wiz,c=us
* o=wiz,c=us
*
* If an error is encountered then zero is returned, otherwise a string
* URL is returned. The caller should free the returned string if it is
* non-zero.
*/
char *
char *dn,
int nameparts
)
{
char *url = 0;
char **vals;
/*
* Search for a URL in the named entry or its parent entry.
* Continue until only 2 nameparts remain.
*/
/* search for the labeledURI attribute */
/* locate the first entry returned */
/* locate the labeledURI attribute */
if ((vals =
NULL) {
/* copy the attribute value */
NULL) {
}
}
}
/* free the search results */
}
}
if (! url) {
/* advance along the DN by one namepart */
next_dn++;
nameparts--;
}
}
}
return (url);
}
#endif /* _SOLARIS_SDK */