/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* 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 [ ? extensions ] ] ] ]
*
* 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
* extensions is a comma separated list of extension
*
* 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>
*/
#ifndef lint
static char copyright[] = "@(#) Copyright (c) 1996 Regents of the University of Michigan.\nAll rights reserved.\n";
#endif
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#ifdef MACOS
#include <stdlib.h>
#include "macos.h"
#endif /* MACOS */
#include <stdlib.h>
#include <malloc.h>
#include "msdos.h"
#endif /* DOS || _WIN32 */
#endif /* !MACOS && !DOS && !_WIN32 */
#include "lber.h"
#include "ldap.h"
#include "ldap-private.h"
#include "ldap-int.h"
#ifdef NEEDPROTOS
static void hex_unescape( char *s );
static int unhex( char c );
#else /* NEEDPROTOS */
static int skip_url_prefix();
static void hex_unescape();
static int unhex();
#endif /* NEEDPROTOS */
int
{
int enclosed;
}
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
*/
return( 0 );
}
/* skip leading '<' (if any) */
if ( **urlp == '<' ) {
*enclosedp = 1;
++*urlp;
} else {
*enclosedp = 0;
}
/* skip leading "URL:" (if any) */
}
/* check for missing "ldap://" prefix */
return( 0 );
}
/* skip over "ldap://" prefix and return success */
*urlp += LDAP_URL_PREFIX_LEN;
return( 1 );
}
{
/* Pick apart the pieces of an LDAP URL Extensions */
/* No copy of exts is made, LDAPURLExt's points to exts string */
int i = 0;
char *p = exts;
/* Count the number of , in extensions */
i++;
}
/* There are at most i+1 extensions */
return (LDAP_URL_ERR_MEM);
}
p = exts;
i = 0;
while ( p ) {
*ptr++ = '\0';
else
return (LDAP_URL_ERR_MEM);
}
if (*p == '!'){
p++;
}
*ptr2++ = '\0';
}
i++;
p = ptr;
}
return( 0 );
}
int
{
/*
* Pick apart the pieces of an LDAP URL.
*/
char *p = NULL;
char *q = NULL;
char *x = NULL;
return( LDAP_URL_ERR_NOTLDAP );
}
/* allocate return struct */
== NULLLDAPURLDESC ) {
return( LDAP_URL_ERR_MEM );
}
/* make working copy of the remainder of the URL */
return( LDAP_URL_ERR_MEM );
}
*p = '\0';
}
/* set defaults */
/* LP By default don't set them... Then we can check if they are present or not in URL */
/* lud_string is the only malloc'd string space we use */
/* scan forward for '/' that marks end of hostport and begin. of dn */
}
/* terminate hostport; point to start of dn */
*p++ = '\0';
}
if ( *url == '\0' ) {
} else {
}
/* 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';
/* terminate scope; point to start of filter */
*q++ = '\0';
/* terminate filter; point to start of extension */
*x++ = '\0';
if (*x != '\0'){
/* parse extensions */
}
}
if ( *q != '\0' ) {
ludp->lud_filter = q;
}
}
if ( strcasecmp( p, "one" ) == 0 ) {
} else if ( strcasecmp( p, "base" ) == 0 ) {
} else if ( strcasecmp( p, "sub" ) == 0 ) {
} else if ( *p != '\0' ) {
return( LDAP_URL_ERR_BADSCOPE );
}
}
}
} else {
}
/*
* 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 (x != NULL && *x != '\0'){
return ( errcode );
}
}
}
return( 0 );
}
{
int i;
}
}
void
{
if ( ludp != NULLLDAPURLDESC ) {
}
}
}
}
}
int
{
int err;
#ifdef _REENTRANT
#endif
#ifdef _REENTRANT
#endif
return( -1 );
}
#ifdef _REENTRANT
#endif
return( -1 );
}
err = 0;
}
err = -1;
} else {
} else {
}
}
}
if ( err != 0 ) {
} else {
}
#ifdef _REENTRANT
#endif
return( err );
}
int
{
int msgid;
}
}
}
#ifdef _REENTRANT
#endif
if (retcode == LDAP_SUCCESS)
#ifdef _REENTRANT
#endif
return (retcode);
}
int
{
int msgid;
}
}
#ifdef _REENTRANT
#endif
if (retcode == LDAP_SUCCESS)
#ifdef _REENTRANT
#endif
return (retcode);
}
static void
hex_unescape( char *s )
{
/*
* Remove URL hex escapes from s... done in place. The basic concept for
* this routine is borrowed from the WWW library HTUnEscape() routine.
*/
char *p;
for ( p = s; *s != '\0'; ++s ) {
if ( *s == '%' ) {
if ( *++s != '\0' ) {
*p = unhex( *s ) << 4;
}
if ( *++s != '\0' ) {
*p++ += unhex( *s );
}
} else {
*p++ = *s;
}
}
*p = '\0';
}
static int
unhex( char c )
{
return( c >= '0' && c <= '9' ? c - '0'
: c >= 'A' && c <= 'F' ? c - 'A' + 10
: c - 'a' + 10 );
}
/*
* 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);
}