17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright 2001-2003 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the Netscape Public
87c478bd9Sstevel@tonic-gate  * License Version 1.1 (the "License"); you may not use this file
97c478bd9Sstevel@tonic-gate  * except in compliance with the License. You may obtain a copy of
107c478bd9Sstevel@tonic-gate  * the License at http://www.mozilla.org/NPL/
117c478bd9Sstevel@tonic-gate  *
127c478bd9Sstevel@tonic-gate  * Software distributed under the License is distributed on an "AS
137c478bd9Sstevel@tonic-gate  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
147c478bd9Sstevel@tonic-gate  * implied. See the License for the specific language governing
157c478bd9Sstevel@tonic-gate  * rights and limitations under the License.
167c478bd9Sstevel@tonic-gate  *
177c478bd9Sstevel@tonic-gate  * The Original Code is Mozilla Communicator client code, released
187c478bd9Sstevel@tonic-gate  * March 31, 1998.
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * The Initial Developer of the Original Code is Netscape
217c478bd9Sstevel@tonic-gate  * Communications Corporation. Portions created by Netscape are
227c478bd9Sstevel@tonic-gate  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
237c478bd9Sstevel@tonic-gate  * Rights Reserved.
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  * Contributor(s):
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  *  Copyright (c) 1996 Regents of the University of Michigan.
297c478bd9Sstevel@tonic-gate  *  All rights reserved.
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate /*  LIBLDAP url.c -- LDAP URL related routines
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  *  LDAP URLs look like this:
357c478bd9Sstevel@tonic-gate  *    l d a p : / / hostport / dn [ ? attributes [ ? scope [ ? filter ] ] ]
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  *  where:
387c478bd9Sstevel@tonic-gate  *   attributes is a comma separated list
397c478bd9Sstevel@tonic-gate  *   scope is one of these three strings:  base one sub (default=base)
407c478bd9Sstevel@tonic-gate  *   filter is an string-represented filter as in RFC 1558
417c478bd9Sstevel@tonic-gate  *
427c478bd9Sstevel@tonic-gate  *  e.g.,  ldap://ldap.itd.umich.edu/c=US?o,description?one?o=umich
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  *  We also tolerate URLs that look like: <ldapurl> and <URL:ldapurl>
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #if 0
48*1da57d55SToomas Soome #ifndef lint
497c478bd9Sstevel@tonic-gate static char copyright[] = "@(#) Copyright (c) 1996 Regents of the University of Michigan.\nAll rights reserved.\n";
507c478bd9Sstevel@tonic-gate #endif
517c478bd9Sstevel@tonic-gate #endif
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #include "ldap-int.h"
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate static int skip_url_prefix( const char **urlp, int *enclosedp, int *securep );
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate int
607c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_is_ldap_url(const char * url)617c478bd9Sstevel@tonic-gate ldap_is_ldap_url( const char *url )
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate 	int	enclosed, secure;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	return( url != NULL
667c478bd9Sstevel@tonic-gate 	    && skip_url_prefix( &url, &enclosed, &secure ));
677c478bd9Sstevel@tonic-gate }
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate static int
skip_url_prefix(const char ** urlp,int * enclosedp,int * securep)717c478bd9Sstevel@tonic-gate skip_url_prefix( const char **urlp, int *enclosedp, int *securep )
727c478bd9Sstevel@tonic-gate {
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate  * return non-zero if this looks like a LDAP URL; zero if not
757c478bd9Sstevel@tonic-gate  * if non-zero returned, *urlp will be moved past "ldap://" part of URL
767c478bd9Sstevel@tonic-gate  * The data that *urlp points to is not changed by this function.
777c478bd9Sstevel@tonic-gate  */
787c478bd9Sstevel@tonic-gate 	if ( *urlp == NULL ) {
797c478bd9Sstevel@tonic-gate 		return( 0 );
807c478bd9Sstevel@tonic-gate 	}
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	/* skip leading '<' (if any) */
837c478bd9Sstevel@tonic-gate 	if ( **urlp == '<' ) {
847c478bd9Sstevel@tonic-gate 		*enclosedp = 1;
857c478bd9Sstevel@tonic-gate 		++*urlp;
867c478bd9Sstevel@tonic-gate 	} else {
877c478bd9Sstevel@tonic-gate 		*enclosedp = 0;
887c478bd9Sstevel@tonic-gate 	}
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	/* skip leading "URL:" (if any) */
917c478bd9Sstevel@tonic-gate 	if ( strlen( *urlp ) >= LDAP_URL_URLCOLON_LEN && strncasecmp(
927c478bd9Sstevel@tonic-gate 	    *urlp, LDAP_URL_URLCOLON, LDAP_URL_URLCOLON_LEN ) == 0 ) {
937c478bd9Sstevel@tonic-gate 		*urlp += LDAP_URL_URLCOLON_LEN;
947c478bd9Sstevel@tonic-gate 	}
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	/* check for an "ldap://" prefix */
977c478bd9Sstevel@tonic-gate 	if ( strlen( *urlp ) >= LDAP_URL_PREFIX_LEN && strncasecmp( *urlp,
987c478bd9Sstevel@tonic-gate 	    LDAP_URL_PREFIX, LDAP_URL_PREFIX_LEN ) == 0 ) {
997c478bd9Sstevel@tonic-gate 		/* skip over URL prefix and return success */
1007c478bd9Sstevel@tonic-gate 		*urlp += LDAP_URL_PREFIX_LEN;
1017c478bd9Sstevel@tonic-gate 		*securep = 0;
1027c478bd9Sstevel@tonic-gate 		return( 1 );
1037c478bd9Sstevel@tonic-gate 	}
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	/* check for an "ldaps://" prefix */
1067c478bd9Sstevel@tonic-gate 	if ( strlen( *urlp ) >= LDAPS_URL_PREFIX_LEN && strncasecmp( *urlp,
1077c478bd9Sstevel@tonic-gate 	    LDAPS_URL_PREFIX, LDAPS_URL_PREFIX_LEN ) == 0 ) {
1087c478bd9Sstevel@tonic-gate 		/* skip over URL prefix and return success */
1097c478bd9Sstevel@tonic-gate 		*urlp += LDAPS_URL_PREFIX_LEN;
1107c478bd9Sstevel@tonic-gate 		*securep = 1;
1117c478bd9Sstevel@tonic-gate 		return( 1 );
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	return( 0 );	/* not an LDAP URL */
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate int
1197c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_url_parse(const char * url,LDAPURLDesc ** ludpp)1207c478bd9Sstevel@tonic-gate ldap_url_parse( const char *url, LDAPURLDesc **ludpp )
1217c478bd9Sstevel@tonic-gate {
1227c478bd9Sstevel@tonic-gate /*
1237c478bd9Sstevel@tonic-gate  *  Pick apart the pieces of an LDAP URL.
1247c478bd9Sstevel@tonic-gate  */
1257c478bd9Sstevel@tonic-gate 	int	rc;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	if (( rc = nsldapi_url_parse( url, ludpp, 1 )) == 0 ) {
1287c478bd9Sstevel@tonic-gate 		if ( (*ludpp)->lud_scope == -1 ) {
1297c478bd9Sstevel@tonic-gate 			(*ludpp)->lud_scope = LDAP_SCOPE_BASE;
1307c478bd9Sstevel@tonic-gate 		}
1317c478bd9Sstevel@tonic-gate 		if ( (*ludpp)->lud_filter == NULL ) {
1327c478bd9Sstevel@tonic-gate 			(*ludpp)->lud_filter = "(objectclass=*)";
1337c478bd9Sstevel@tonic-gate 		}
1347c478bd9Sstevel@tonic-gate 		if ( *((*ludpp)->lud_dn) == '\0' ) {
1357c478bd9Sstevel@tonic-gate 			(*ludpp)->lud_dn = NULL;
1367c478bd9Sstevel@tonic-gate 		}
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	return( rc );
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate /* same as ldap_url_parse(), but dn is not require */
1437c478bd9Sstevel@tonic-gate int
1447c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_url_parse_nodn(const char * url,LDAPURLDesc ** ludpp)1457c478bd9Sstevel@tonic-gate ldap_url_parse_nodn(const char *url, LDAPURLDesc **ludpp)
1467c478bd9Sstevel@tonic-gate {
1477c478bd9Sstevel@tonic-gate /*
1487c478bd9Sstevel@tonic-gate  *  Pick apart the pieces of an LDAP URL.
1497c478bd9Sstevel@tonic-gate  */
1507c478bd9Sstevel@tonic-gate 	int	rc;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	if ((rc = nsldapi_url_parse(url, ludpp, 0)) == 0) {
1537c478bd9Sstevel@tonic-gate 		if ((*ludpp)->lud_scope == -1) {
1547c478bd9Sstevel@tonic-gate 			(*ludpp)->lud_scope = LDAP_SCOPE_BASE;
1557c478bd9Sstevel@tonic-gate 		}
1567c478bd9Sstevel@tonic-gate 		if ((*ludpp)->lud_filter == NULL) {
1577c478bd9Sstevel@tonic-gate 			(*ludpp)->lud_filter = "(objectclass=*)";
1587c478bd9Sstevel@tonic-gate 		}
1597c478bd9Sstevel@tonic-gate 		if ((*ludpp)->lud_dn && *((*ludpp)->lud_dn) == '\0') {
1607c478bd9Sstevel@tonic-gate 			(*ludpp)->lud_dn = NULL;
1617c478bd9Sstevel@tonic-gate 		}
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	return (rc);
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /*
1697c478bd9Sstevel@tonic-gate  * like ldap_url_parse() with a few exceptions:
1707c478bd9Sstevel@tonic-gate  *   1) if dn_required is zero, a missing DN does not generate an error
1717c478bd9Sstevel@tonic-gate  *	(we just leave the lud_dn field NULL)
1727c478bd9Sstevel@tonic-gate  *   2) no defaults are set for lud_scope and lud_filter (they are set to -1
1737c478bd9Sstevel@tonic-gate  *	and NULL respectively if no SCOPE or FILTER are present in the URL).
1747c478bd9Sstevel@tonic-gate  *   3) when there is a zero-length DN in a URL we do not set lud_dn to NULL.
175*1da57d55SToomas Soome  *   4) if an LDAPv3 URL extensions are included,
1767c478bd9Sstevel@tonic-gate  */
1777c478bd9Sstevel@tonic-gate int
nsldapi_url_parse(const char * url,LDAPURLDesc ** ludpp,int dn_required)1787c478bd9Sstevel@tonic-gate nsldapi_url_parse( const char *url, LDAPURLDesc **ludpp, int dn_required )
1797c478bd9Sstevel@tonic-gate {
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	LDAPURLDesc	*ludp;
1827c478bd9Sstevel@tonic-gate 	char		*urlcopy, *attrs, *scope, *extensions = NULL, *p, *q;
1837c478bd9Sstevel@tonic-gate 	int		enclosed, secure, i, nattrs, at_start;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	LDAPDebug( LDAP_DEBUG_TRACE, "nsldapi_url_parse(%s)\n", url, 0, 0 );
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	if ( url == NULL || ludpp == NULL ) {
1887c478bd9Sstevel@tonic-gate 		return( LDAP_URL_ERR_PARAM );
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	*ludpp = NULL;	/* pessimistic */
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	if ( !skip_url_prefix( &url, &enclosed, &secure )) {
1947c478bd9Sstevel@tonic-gate 		return( LDAP_URL_ERR_NOTLDAP );
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	/* allocate return struct */
1987c478bd9Sstevel@tonic-gate 	if (( ludp = (LDAPURLDesc *)NSLDAPI_CALLOC( 1, sizeof( LDAPURLDesc )))
1997c478bd9Sstevel@tonic-gate 	    == NULLLDAPURLDESC ) {
2007c478bd9Sstevel@tonic-gate 		return( LDAP_URL_ERR_MEM );
2017c478bd9Sstevel@tonic-gate 	}
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	if ( secure ) {
2047c478bd9Sstevel@tonic-gate 		ludp->lud_options |= LDAP_URL_OPT_SECURE;
2057c478bd9Sstevel@tonic-gate 	}
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	/* make working copy of the remainder of the URL */
2087c478bd9Sstevel@tonic-gate 	if (( urlcopy = nsldapi_strdup( url )) == NULL ) {
2097c478bd9Sstevel@tonic-gate 		ldap_free_urldesc( ludp );
2107c478bd9Sstevel@tonic-gate 		return( LDAP_URL_ERR_MEM );
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	if ( enclosed && *((p = urlcopy + strlen( urlcopy ) - 1)) == '>' ) {
2147c478bd9Sstevel@tonic-gate 		*p = '\0';
2157c478bd9Sstevel@tonic-gate 	}
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	/* initialize scope and filter */
2187c478bd9Sstevel@tonic-gate 	ludp->lud_scope = -1;
2197c478bd9Sstevel@tonic-gate 	ludp->lud_filter = NULL;
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	/* lud_string is the only malloc'd string space we use */
2227c478bd9Sstevel@tonic-gate 	ludp->lud_string = urlcopy;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	/* scan forward for '/' that marks end of hostport and begin. of dn */
2257c478bd9Sstevel@tonic-gate 	if (( ludp->lud_dn = strchr( urlcopy, '/' )) == NULL ) {
2267c478bd9Sstevel@tonic-gate 		if ( dn_required ) {
2277c478bd9Sstevel@tonic-gate 			ldap_free_urldesc( ludp );
2287c478bd9Sstevel@tonic-gate 			return( LDAP_URL_ERR_NODN );
2297c478bd9Sstevel@tonic-gate 		}
2307c478bd9Sstevel@tonic-gate 	} else {
2317c478bd9Sstevel@tonic-gate 		/* terminate hostport; point to start of dn */
2327c478bd9Sstevel@tonic-gate 		*ludp->lud_dn++ = '\0';
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	if ( *urlcopy == '\0' ) {
2377c478bd9Sstevel@tonic-gate 		ludp->lud_host = NULL;
2387c478bd9Sstevel@tonic-gate 	} else {
2397c478bd9Sstevel@tonic-gate 		ludp->lud_host = urlcopy;
2407c478bd9Sstevel@tonic-gate 		nsldapi_hex_unescape( ludp->lud_host );
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 		/*
2437c478bd9Sstevel@tonic-gate 		 * Locate and strip off optional port number (:#) in host
2447c478bd9Sstevel@tonic-gate 		 * portion of URL.
2457c478bd9Sstevel@tonic-gate 		 *
2467c478bd9Sstevel@tonic-gate 		 * If more than one space-separated host is listed, we only
2477c478bd9Sstevel@tonic-gate 		 * look for a port number within the right-most one since
2487c478bd9Sstevel@tonic-gate 		 * ldap_init() will handle host parameters that look like
2497c478bd9Sstevel@tonic-gate 		 * host:port anyway.
2507c478bd9Sstevel@tonic-gate 		 */
2517c478bd9Sstevel@tonic-gate 		if (( p = strrchr( ludp->lud_host, ' ' )) == NULL ) {
2527c478bd9Sstevel@tonic-gate 			p = ludp->lud_host;
2537c478bd9Sstevel@tonic-gate 		} else {
2547c478bd9Sstevel@tonic-gate 			++p;
2557c478bd9Sstevel@tonic-gate 		}
2567c478bd9Sstevel@tonic-gate                 if ( *p == '[' && ( q = strchr( p, ']' )) != NULL ) {
2577c478bd9Sstevel@tonic-gate                          /* square brackets present -- skip past them */
2587c478bd9Sstevel@tonic-gate                         p = q++;
2597c478bd9Sstevel@tonic-gate                 }
2607c478bd9Sstevel@tonic-gate 		if (( p = strchr( p, ':' )) != NULL ) {
2617c478bd9Sstevel@tonic-gate 			*p++ = '\0';
2627c478bd9Sstevel@tonic-gate 			ludp->lud_port = atoi( p );
2637c478bd9Sstevel@tonic-gate 			if ( *ludp->lud_host == '\0' ) {
2647c478bd9Sstevel@tonic-gate 				/*
2657c478bd9Sstevel@tonic-gate 				 * no hostname and a port: invalid hostcode
2667c478bd9Sstevel@tonic-gate 				 * according to RFC 1738
2677c478bd9Sstevel@tonic-gate 				 */
2687c478bd9Sstevel@tonic-gate 				ldap_free_urldesc(ludp);
2697c478bd9Sstevel@tonic-gate 				return (LDAP_URL_ERR_HOSTPORT);
2707c478bd9Sstevel@tonic-gate 			}
2717c478bd9Sstevel@tonic-gate 		}
2727c478bd9Sstevel@tonic-gate 	}
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	/* scan for '?' that marks end of dn and beginning of attributes */
2757c478bd9Sstevel@tonic-gate 	attrs = NULL;
2767c478bd9Sstevel@tonic-gate 	if ( ludp->lud_dn != NULL &&
2777c478bd9Sstevel@tonic-gate 	    ( attrs = strchr( ludp->lud_dn, '?' )) != NULL ) {
2787c478bd9Sstevel@tonic-gate 		/* terminate dn; point to start of attrs. */
2797c478bd9Sstevel@tonic-gate 		*attrs++ = '\0';
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 		/* scan for '?' that marks end of attrs and begin. of scope */
2827c478bd9Sstevel@tonic-gate 		if (( p = strchr( attrs, '?' )) != NULL ) {
2837c478bd9Sstevel@tonic-gate 			/*
2847c478bd9Sstevel@tonic-gate 			 * terminate attrs; point to start of scope and scan for
2857c478bd9Sstevel@tonic-gate 			 * '?' that marks end of scope and begin. of filter
2867c478bd9Sstevel@tonic-gate 			 */
2877c478bd9Sstevel@tonic-gate 			*p++ = '\0';
2887c478bd9Sstevel@tonic-gate                         scope = p;
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate                         if (( p = strchr( scope, '?' )) != NULL ) {
2917c478bd9Sstevel@tonic-gate                                 /* terminate scope; point to start of filter */
2927c478bd9Sstevel@tonic-gate                                 *p++ = '\0';
2937c478bd9Sstevel@tonic-gate                                 if ( *p != '\0' ) {
2947c478bd9Sstevel@tonic-gate                                         ludp->lud_filter = p;
2957c478bd9Sstevel@tonic-gate                                         /*
2967c478bd9Sstevel@tonic-gate                                          * scan for the '?' that marks the end
2977c478bd9Sstevel@tonic-gate                                          * of the filter and the start of any
2987c478bd9Sstevel@tonic-gate                                          * extensions
2997c478bd9Sstevel@tonic-gate                                          */
3007c478bd9Sstevel@tonic-gate                                         if (( p = strchr( ludp->lud_filter, '?' ))
3017c478bd9Sstevel@tonic-gate                                             != NULL ) {
3027c478bd9Sstevel@tonic-gate                                                 *p++ = '\0'; /* term. filter */
3037c478bd9Sstevel@tonic-gate                                                 extensions = p;
3047c478bd9Sstevel@tonic-gate                                         }
3057c478bd9Sstevel@tonic-gate                                         if ( *ludp->lud_filter == '\0' ) {
3067c478bd9Sstevel@tonic-gate                                                 ludp->lud_filter = NULL;
3077c478bd9Sstevel@tonic-gate                                         } else {
3087c478bd9Sstevel@tonic-gate                                                 nsldapi_hex_unescape( ludp->lud_filter );
3097c478bd9Sstevel@tonic-gate                                         }
3107c478bd9Sstevel@tonic-gate                                 }
3117c478bd9Sstevel@tonic-gate                         }
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate                         if ( strcasecmp( scope, "one" ) == 0 ) {
3157c478bd9Sstevel@tonic-gate                                 ludp->lud_scope = LDAP_SCOPE_ONELEVEL;
3167c478bd9Sstevel@tonic-gate                         } else if ( strcasecmp( scope, "base" ) == 0 ) {
3177c478bd9Sstevel@tonic-gate                                 ludp->lud_scope = LDAP_SCOPE_BASE;
3187c478bd9Sstevel@tonic-gate                         } else if ( strcasecmp( scope, "sub" ) == 0 ) {
3197c478bd9Sstevel@tonic-gate                                 ludp->lud_scope = LDAP_SCOPE_SUBTREE;
3207c478bd9Sstevel@tonic-gate                         } else if ( *scope != '\0' ) {
3217c478bd9Sstevel@tonic-gate                                 ldap_free_urldesc( ludp );
3227c478bd9Sstevel@tonic-gate                                 return( LDAP_URL_ERR_BADSCOPE );
3237c478bd9Sstevel@tonic-gate                         }
3247c478bd9Sstevel@tonic-gate 		}
3257c478bd9Sstevel@tonic-gate 	}
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	if ( ludp->lud_dn != NULL ) {
3287c478bd9Sstevel@tonic-gate 		nsldapi_hex_unescape( ludp->lud_dn );
3297c478bd9Sstevel@tonic-gate 	}
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	/*
3327c478bd9Sstevel@tonic-gate 	 * if attrs list was included, turn it into a null-terminated array
3337c478bd9Sstevel@tonic-gate 	 */
3347c478bd9Sstevel@tonic-gate 	if ( attrs != NULL && *attrs != '\0' ) {
3357c478bd9Sstevel@tonic-gate 		nsldapi_hex_unescape( attrs );
3367c478bd9Sstevel@tonic-gate 		for ( nattrs = 1, p = attrs; *p != '\0'; ++p ) {
3377c478bd9Sstevel@tonic-gate 		    if ( *p == ',' ) {
3387c478bd9Sstevel@tonic-gate 			    ++nattrs;
3397c478bd9Sstevel@tonic-gate 		    }
3407c478bd9Sstevel@tonic-gate 		}
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 		if (( ludp->lud_attrs = (char **)NSLDAPI_CALLOC( nattrs + 1,
3437c478bd9Sstevel@tonic-gate 		    sizeof( char * ))) == NULL ) {
3447c478bd9Sstevel@tonic-gate 			ldap_free_urldesc( ludp );
3457c478bd9Sstevel@tonic-gate 			return( LDAP_URL_ERR_MEM );
3467c478bd9Sstevel@tonic-gate 		}
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 		for ( i = 0, p = attrs; i < nattrs; ++i ) {
3497c478bd9Sstevel@tonic-gate 			ludp->lud_attrs[ i ] = p;
3507c478bd9Sstevel@tonic-gate 			if (( p = strchr( p, ',' )) != NULL ) {
3517c478bd9Sstevel@tonic-gate 				*p++ ='\0';
3527c478bd9Sstevel@tonic-gate 			}
3537c478bd9Sstevel@tonic-gate 			nsldapi_hex_unescape( ludp->lud_attrs[ i ] );
3547c478bd9Sstevel@tonic-gate 		}
3557c478bd9Sstevel@tonic-gate 	}
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate         /* if extensions list was included, check for critical ones */
3587c478bd9Sstevel@tonic-gate         if ( extensions != NULL && *extensions != '\0' ) {
3597c478bd9Sstevel@tonic-gate                 /* Note: at present, we do not recognize ANY extensions */
3607c478bd9Sstevel@tonic-gate                 at_start = 1;
3617c478bd9Sstevel@tonic-gate                 for ( p = extensions; *p != '\0'; ++p ) {
3627c478bd9Sstevel@tonic-gate                         if ( at_start ) {
3637c478bd9Sstevel@tonic-gate                                 if ( *p == '!' ) {      /* critical extension */
3647c478bd9Sstevel@tonic-gate                                         ldap_free_urldesc( ludp );
3657c478bd9Sstevel@tonic-gate                                         /* this is what iplanet did *
3667c478bd9Sstevel@tonic-gate                                         return( LDAP_URL_UNRECOGNIZED_CRITICAL_EXTENSION );
3677c478bd9Sstevel@tonic-gate                                          * and this is what we do */
3687c478bd9Sstevel@tonic-gate                                         return( LDAP_URL_ERR_PARAM );
3697c478bd9Sstevel@tonic-gate                                 }
3707c478bd9Sstevel@tonic-gate                                 at_start = 0;
3717c478bd9Sstevel@tonic-gate                         } else if ( *p == ',' ) {
3727c478bd9Sstevel@tonic-gate                                 at_start = 1;
3737c478bd9Sstevel@tonic-gate                         }
3747c478bd9Sstevel@tonic-gate                 }
3757c478bd9Sstevel@tonic-gate         }
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	*ludpp = ludp;
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	return( 0 );
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate void
3857c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_free_urldesc(LDAPURLDesc * ludp)3867c478bd9Sstevel@tonic-gate ldap_free_urldesc( LDAPURLDesc *ludp )
3877c478bd9Sstevel@tonic-gate {
3887c478bd9Sstevel@tonic-gate 	if ( ludp != NULLLDAPURLDESC ) {
3897c478bd9Sstevel@tonic-gate 		if ( ludp->lud_string != NULL ) {
3907c478bd9Sstevel@tonic-gate 			NSLDAPI_FREE( ludp->lud_string );
3917c478bd9Sstevel@tonic-gate 		}
3927c478bd9Sstevel@tonic-gate 		if ( ludp->lud_attrs != NULL ) {
3937c478bd9Sstevel@tonic-gate 			NSLDAPI_FREE( ludp->lud_attrs );
3947c478bd9Sstevel@tonic-gate 		}
3957c478bd9Sstevel@tonic-gate 		NSLDAPI_FREE( ludp );
3967c478bd9Sstevel@tonic-gate 	}
3977c478bd9Sstevel@tonic-gate }
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate int
4017c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_url_search(LDAP * ld,const char * url,int attrsonly)4027c478bd9Sstevel@tonic-gate ldap_url_search( LDAP *ld, const char *url, int attrsonly )
4037c478bd9Sstevel@tonic-gate {
4047c478bd9Sstevel@tonic-gate 	int		err, msgid;
4057c478bd9Sstevel@tonic-gate 	LDAPURLDesc	*ludp;
4067c478bd9Sstevel@tonic-gate 	BerElement	*ber;
4077c478bd9Sstevel@tonic-gate 	LDAPServer	*srv;
4087c478bd9Sstevel@tonic-gate 	char		*host;
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
4117c478bd9Sstevel@tonic-gate 		return( -1 );		/* punt */
4127c478bd9Sstevel@tonic-gate 	}
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	if ( ldap_url_parse( url, &ludp ) != 0 ) {
4157c478bd9Sstevel@tonic-gate 		LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL );
4167c478bd9Sstevel@tonic-gate 		return( -1 );
4177c478bd9Sstevel@tonic-gate 	}
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	LDAP_MUTEX_LOCK( ld, LDAP_MSGID_LOCK );
4207c478bd9Sstevel@tonic-gate 	msgid = ++ld->ld_msgid;
4217c478bd9Sstevel@tonic-gate 	LDAP_MUTEX_UNLOCK( ld, LDAP_MSGID_LOCK );
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	if ( nsldapi_build_search_req( ld, ludp->lud_dn, ludp->lud_scope,
4247c478bd9Sstevel@tonic-gate 	    ludp->lud_filter, ludp->lud_attrs, attrsonly, NULL, NULL,
4257c478bd9Sstevel@tonic-gate 	    -1, -1, msgid, &ber ) != LDAP_SUCCESS ) {
4267c478bd9Sstevel@tonic-gate 		return( -1 );
4277c478bd9Sstevel@tonic-gate 	}
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	err = 0;
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	if ( ludp->lud_host == NULL ) {
4327c478bd9Sstevel@tonic-gate 		host = ld->ld_defhost;
4337c478bd9Sstevel@tonic-gate 	} else {
4347c478bd9Sstevel@tonic-gate 		host = ludp->lud_host;
4357c478bd9Sstevel@tonic-gate 	}
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	if (( srv = (LDAPServer *)NSLDAPI_CALLOC( 1, sizeof( LDAPServer )))
4387c478bd9Sstevel@tonic-gate 	    == NULL || ( host != NULL &&
4397c478bd9Sstevel@tonic-gate 	    ( srv->lsrv_host = nsldapi_strdup( host )) == NULL )) {
4407c478bd9Sstevel@tonic-gate 		if ( srv != NULL ) {
4417c478bd9Sstevel@tonic-gate 			NSLDAPI_FREE( srv );
4427c478bd9Sstevel@tonic-gate 		}
4437c478bd9Sstevel@tonic-gate 		LDAP_SET_LDERRNO( ld, LDAP_NO_MEMORY, NULL, NULL );
4447c478bd9Sstevel@tonic-gate 		err = -1;
4457c478bd9Sstevel@tonic-gate 	} else {
4467c478bd9Sstevel@tonic-gate                 if ( ludp->lud_port != 0 ) {
4477c478bd9Sstevel@tonic-gate                         /* URL includes a port - use it */
4487c478bd9Sstevel@tonic-gate                          srv->lsrv_port = ludp->lud_port;
4497c478bd9Sstevel@tonic-gate                 } else if ( ludp->lud_host == NULL ) {
4507c478bd9Sstevel@tonic-gate                         /* URL has no port or host - use port from ld */
4517c478bd9Sstevel@tonic-gate                         srv->lsrv_port = ld->ld_defport;
4527c478bd9Sstevel@tonic-gate                 } else if (( ludp->lud_options & LDAP_URL_OPT_SECURE ) == 0 ) {
4537c478bd9Sstevel@tonic-gate                         /* ldap URL has a host but no port - use std. port */
4547c478bd9Sstevel@tonic-gate                         srv->lsrv_port = LDAP_PORT;
4557c478bd9Sstevel@tonic-gate                 } else {
4567c478bd9Sstevel@tonic-gate                         /* ldaps URL has a host but no port - use std. port */
4577c478bd9Sstevel@tonic-gate                         srv->lsrv_port = LDAPS_PORT;
4587c478bd9Sstevel@tonic-gate                 }
4597c478bd9Sstevel@tonic-gate 	}
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	if (( ludp->lud_options & LDAP_URL_OPT_SECURE ) != 0 ) {
4627c478bd9Sstevel@tonic-gate 		srv->lsrv_options |= LDAP_SRV_OPT_SECURE;
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	if ( err != 0 ) {
4667c478bd9Sstevel@tonic-gate 		ber_free( ber, 1 );
4677c478bd9Sstevel@tonic-gate 	} else {
4687c478bd9Sstevel@tonic-gate 		err = nsldapi_send_server_request( ld, ber, msgid, NULL, srv,
4697c478bd9Sstevel@tonic-gate 		    NULL, NULL, 1 );
4707c478bd9Sstevel@tonic-gate 	}
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	ldap_free_urldesc( ludp );
4737c478bd9Sstevel@tonic-gate 	return( err );
4747c478bd9Sstevel@tonic-gate }
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate int
4787c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_url_search_st(LDAP * ld,const char * url,int attrsonly,struct timeval * timeout,LDAPMessage ** res)4797c478bd9Sstevel@tonic-gate ldap_url_search_st( LDAP *ld, const char *url, int attrsonly,
4807c478bd9Sstevel@tonic-gate 	struct timeval *timeout, LDAPMessage **res )
4817c478bd9Sstevel@tonic-gate {
4827c478bd9Sstevel@tonic-gate 	int	msgid;
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	/*
4857c478bd9Sstevel@tonic-gate 	 * It is an error to pass in a zero'd timeval.
4867c478bd9Sstevel@tonic-gate 	 */
4877c478bd9Sstevel@tonic-gate 	if ( timeout != NULL && timeout->tv_sec == 0 &&
4887c478bd9Sstevel@tonic-gate 	    timeout->tv_usec == 0 ) {
4897c478bd9Sstevel@tonic-gate 		if ( ld != NULL ) {
4907c478bd9Sstevel@tonic-gate 			LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL );
4917c478bd9Sstevel@tonic-gate 		}
4927c478bd9Sstevel@tonic-gate 		if ( res != NULL ) {
4937c478bd9Sstevel@tonic-gate 			*res = NULL;
4947c478bd9Sstevel@tonic-gate 		}
4957c478bd9Sstevel@tonic-gate                 return( LDAP_PARAM_ERROR );
4967c478bd9Sstevel@tonic-gate         }
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
4997c478bd9Sstevel@tonic-gate 		return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
5007c478bd9Sstevel@tonic-gate 	}
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 ) {
5037c478bd9Sstevel@tonic-gate 		return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
5047c478bd9Sstevel@tonic-gate 	}
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	if ( LDAP_GET_LDERRNO( ld, NULL, NULL ) == LDAP_TIMEOUT ) {
5077c478bd9Sstevel@tonic-gate 		(void) ldap_abandon( ld, msgid );
5087c478bd9Sstevel@tonic-gate 		LDAP_SET_LDERRNO( ld, LDAP_TIMEOUT, NULL, NULL );
5097c478bd9Sstevel@tonic-gate 		return( LDAP_TIMEOUT );
5107c478bd9Sstevel@tonic-gate 	}
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	return( ldap_result2error( ld, *res, 0 ));
5137c478bd9Sstevel@tonic-gate }
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate int
5177c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_url_search_s(LDAP * ld,const char * url,int attrsonly,LDAPMessage ** res)5187c478bd9Sstevel@tonic-gate ldap_url_search_s( LDAP *ld, const char *url, int attrsonly, LDAPMessage **res )
5197c478bd9Sstevel@tonic-gate {
5207c478bd9Sstevel@tonic-gate 	int	msgid;
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
5237c478bd9Sstevel@tonic-gate 		return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
5247c478bd9Sstevel@tonic-gate 	}
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	if ( ldap_result( ld, msgid, 1, (struct timeval *)NULL, res ) == -1 ) {
5277c478bd9Sstevel@tonic-gate 		return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
5287c478bd9Sstevel@tonic-gate 	}
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	return( ldap_result2error( ld, *res, 0 ));
5317c478bd9Sstevel@tonic-gate }
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate #ifdef _SOLARIS_SDK
5347c478bd9Sstevel@tonic-gate /*
5357c478bd9Sstevel@tonic-gate  * Locate the LDAP URL associated with a DNS domain name.
5367c478bd9Sstevel@tonic-gate  *
5377c478bd9Sstevel@tonic-gate  * The supplied DNS domain name is converted into a distinguished
5387c478bd9Sstevel@tonic-gate  * name. The directory entry specified by that distinguished name
5397c478bd9Sstevel@tonic-gate  * is searched for a labeledURI attribute. If successful then the
5407c478bd9Sstevel@tonic-gate  * LDAP URL is returned. If unsuccessful then that entry's parent
5417c478bd9Sstevel@tonic-gate  * is searched and so on until the target distinguished name is
5427c478bd9Sstevel@tonic-gate  * reduced to only two nameparts.
5437c478bd9Sstevel@tonic-gate  *
5447c478bd9Sstevel@tonic-gate  * For example, if 'ny.eng.wiz.com' is the DNS domain then the
5457c478bd9Sstevel@tonic-gate  * following entries are searched until one succeeds:
5467c478bd9Sstevel@tonic-gate  *              dc=ny,dc=eng,dc=wiz,dc=com
5477c478bd9Sstevel@tonic-gate  *              dc=eng,dc=wiz,dc=com
5487c478bd9Sstevel@tonic-gate  *              dc=wiz,dc=com
5497c478bd9Sstevel@tonic-gate  *
5507c478bd9Sstevel@tonic-gate  * If dns_name is NULL then the environment variable LOCALDOMAIN is used.
5517c478bd9Sstevel@tonic-gate  * If attrs is not NULL then it is appended to the URL's attribute list.
5527c478bd9Sstevel@tonic-gate  * If scope is not NULL then it overrides the URL's scope.
5537c478bd9Sstevel@tonic-gate  * If filter is not NULL then it is merged with the URL's filter.
5547c478bd9Sstevel@tonic-gate  *
5557c478bd9Sstevel@tonic-gate  * If an error is encountered then zero is returned, otherwise a string
5567c478bd9Sstevel@tonic-gate  * URL is returned. The caller should free the returned string if it is
5577c478bd9Sstevel@tonic-gate  * non-zero.
5587c478bd9Sstevel@tonic-gate  */
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate char *
ldap_dns_to_url(LDAP * ld,char * dns_name,char * attrs,char * scope,char * filter)5617c478bd9Sstevel@tonic-gate ldap_dns_to_url(
5627c478bd9Sstevel@tonic-gate         LDAP    *ld,
5637c478bd9Sstevel@tonic-gate         char    *dns_name,
5647c478bd9Sstevel@tonic-gate         char    *attrs,
5657c478bd9Sstevel@tonic-gate         char    *scope,
5667c478bd9Sstevel@tonic-gate         char    *filter
5677c478bd9Sstevel@tonic-gate )
5687c478bd9Sstevel@tonic-gate {
5697c478bd9Sstevel@tonic-gate         char            *dn;
5707c478bd9Sstevel@tonic-gate         char            *url = 0;
5717c478bd9Sstevel@tonic-gate         char            *url2 = 0;
5727c478bd9Sstevel@tonic-gate         LDAPURLDesc     *urldesc;
5737c478bd9Sstevel@tonic-gate         char            *cp;
5747c478bd9Sstevel@tonic-gate         char            *cp2;
5757c478bd9Sstevel@tonic-gate         size_t          attrs_len = 0;
5767c478bd9Sstevel@tonic-gate         size_t          scope_len = 0;
5777c478bd9Sstevel@tonic-gate         size_t          filter_len = 0;
5787c478bd9Sstevel@tonic-gate         int             nameparts;
5797c478bd9Sstevel@tonic-gate         int             no_attrs = 0;
5807c478bd9Sstevel@tonic-gate         int             no_scope = 0;
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate         if (dns_name == 0) {
5837c478bd9Sstevel@tonic-gate                 dns_name = (char *)getenv("LOCALDOMAIN");
5847c478bd9Sstevel@tonic-gate         }
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate         if ((ld == NULL) || ((dn = ldap_dns_to_dn(dns_name, &nameparts)) ==
5877c478bd9Sstevel@tonic-gate             NULL))
5887c478bd9Sstevel@tonic-gate                 return (0);
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate         if ((url = ldap_dn_to_url(ld, dn, nameparts)) == NULL) {
5917c478bd9Sstevel@tonic-gate                 free(dn);
5927c478bd9Sstevel@tonic-gate                 return (0);
5937c478bd9Sstevel@tonic-gate         }
5947c478bd9Sstevel@tonic-gate         free(dn);
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate         /* merge filter and/or scope and/or attributes with URL */
5977c478bd9Sstevel@tonic-gate         if (attrs || scope || filter) {
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate                 if (attrs)
6007c478bd9Sstevel@tonic-gate                         attrs_len = strlen(attrs) + 2; /* for comma and NULL */
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate                 if (scope)
6037c478bd9Sstevel@tonic-gate                         scope_len = strlen(scope) + 1; /* for NULL */
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate                 if (filter)
6067c478bd9Sstevel@tonic-gate                         filter_len = strlen(filter) + 4;
6077c478bd9Sstevel@tonic-gate                             /* for ampersand, parentheses and NULL */
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate                 if (ldap_is_ldap_url(url)) {
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate                         if ((url2 = (char *)malloc(attrs_len + scope_len +
6127c478bd9Sstevel@tonic-gate                             filter_len + strlen(url) + 1)) == NULL) {
6137c478bd9Sstevel@tonic-gate                                 return (0);
6147c478bd9Sstevel@tonic-gate                         }
6157c478bd9Sstevel@tonic-gate                         cp = url;
6167c478bd9Sstevel@tonic-gate                         cp2 = url2;
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate                         /* copy URL scheme, hostname, port number and DN */
6197c478bd9Sstevel@tonic-gate                         while (*cp && (*cp != '?')) {
6207c478bd9Sstevel@tonic-gate                                 *cp2++ = *cp++;
6217c478bd9Sstevel@tonic-gate                         }
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate                         /* handle URL attributes */
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate                         if (*cp == '?') {       /* test first '?' */
6267c478bd9Sstevel@tonic-gate                                 *cp2++ = *cp++; /* copy first '?' */
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate                                 if (*cp == '?') {       /* test second '?' */
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate                                         /* insert supplied attributes */
6317c478bd9Sstevel@tonic-gate                                         if (attrs) {
6327c478bd9Sstevel@tonic-gate                                                 while (*attrs) {
6337c478bd9Sstevel@tonic-gate                                                         *cp2++ = *attrs++;
6347c478bd9Sstevel@tonic-gate                                                 }
6357c478bd9Sstevel@tonic-gate                                         } else {
6367c478bd9Sstevel@tonic-gate                                                 no_attrs = 1;
6377c478bd9Sstevel@tonic-gate                                         }
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate                                 } else {
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate                                         /* copy URL attributes */
6427c478bd9Sstevel@tonic-gate                                         while (*cp && (*cp != '?')) {
6437c478bd9Sstevel@tonic-gate                                                 *cp2++ = *cp++;
6447c478bd9Sstevel@tonic-gate                                         }
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate                                         /* append supplied attributes */
6477c478bd9Sstevel@tonic-gate                                         if (attrs) {
6487c478bd9Sstevel@tonic-gate                                                 *cp2++ = ',';
6497c478bd9Sstevel@tonic-gate                                                 while (*attrs) {
6507c478bd9Sstevel@tonic-gate                                                         *cp2++ = *attrs++;
6517c478bd9Sstevel@tonic-gate                                                 }
6527c478bd9Sstevel@tonic-gate                                         }
6537c478bd9Sstevel@tonic-gate                                 }
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate                         } else {
6567c478bd9Sstevel@tonic-gate                                 /* append supplied attributes */
6577c478bd9Sstevel@tonic-gate                                 if (attrs) {
6587c478bd9Sstevel@tonic-gate                                         *cp2++ = '?';
6597c478bd9Sstevel@tonic-gate                                         while (*attrs) {
6607c478bd9Sstevel@tonic-gate                                                 *cp2++ = *attrs++;
6617c478bd9Sstevel@tonic-gate                                        }
6627c478bd9Sstevel@tonic-gate                                 } else {
6637c478bd9Sstevel@tonic-gate                                         no_attrs = 1;
6647c478bd9Sstevel@tonic-gate                                 }
6657c478bd9Sstevel@tonic-gate                         }
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate                         /* handle URL scope */
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate                         if (*cp == '?') {       /* test second '?' */
6707c478bd9Sstevel@tonic-gate                                 *cp2++ = *cp++; /* copy second '?' */
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate                                 if (*cp == '?') {       /* test third '?' */
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate                                         /* insert supplied scope */
6757c478bd9Sstevel@tonic-gate                                         if (scope) {
6767c478bd9Sstevel@tonic-gate                                                 while (*scope) {
6777c478bd9Sstevel@tonic-gate                                                         *cp2++ = *scope++;
6787c478bd9Sstevel@tonic-gate                                                 }
6797c478bd9Sstevel@tonic-gate                                         } else {
6807c478bd9Sstevel@tonic-gate                                                 no_scope = 1;
6817c478bd9Sstevel@tonic-gate                                         }
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate                                 } else {
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate                                         if (scope) {
6867c478bd9Sstevel@tonic-gate                                                 /* skip over URL scope */
6877c478bd9Sstevel@tonic-gate                                                 while (*cp && (*cp != '?')) {
6887c478bd9Sstevel@tonic-gate                                                         *cp++;
6897c478bd9Sstevel@tonic-gate                                                 }
6907c478bd9Sstevel@tonic-gate                                                 /* insert supplied scope */
6917c478bd9Sstevel@tonic-gate                                                 while (*scope) {
6927c478bd9Sstevel@tonic-gate                                                         *cp2++ = *scope++;
6937c478bd9Sstevel@tonic-gate                                                 }
6947c478bd9Sstevel@tonic-gate                                         } else {
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate                                                 /* copy URL scope */
6977c478bd9Sstevel@tonic-gate                                                 while (*cp && (*cp != '?')) {
6987c478bd9Sstevel@tonic-gate                                                         *cp2++ = *cp++;
6997c478bd9Sstevel@tonic-gate                                                 }
7007c478bd9Sstevel@tonic-gate                                         }
7017c478bd9Sstevel@tonic-gate                                 }
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate                         } else {
7047c478bd9Sstevel@tonic-gate                                 /* append supplied scope */
7057c478bd9Sstevel@tonic-gate                                 if (scope) {
7067c478bd9Sstevel@tonic-gate                                         if (no_attrs) {
7077c478bd9Sstevel@tonic-gate                                                 *cp2++ = '?';
7087c478bd9Sstevel@tonic-gate                                         }
7097c478bd9Sstevel@tonic-gate                                         *cp2++ = '?';
7107c478bd9Sstevel@tonic-gate                                         while (*scope) {
7117c478bd9Sstevel@tonic-gate                                                 *cp2++ = *scope++;
7127c478bd9Sstevel@tonic-gate                                         }
7137c478bd9Sstevel@tonic-gate                                 } else {
7147c478bd9Sstevel@tonic-gate                                         no_scope = 1;
7157c478bd9Sstevel@tonic-gate                                 }
7167c478bd9Sstevel@tonic-gate                         }
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate                         /* handle URL filter */
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate                         if (*cp == '?') {       /* test third '?' */
7217c478bd9Sstevel@tonic-gate                                 *cp2++ = *cp++; /* copy third '?' */
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate                                 if (filter) {
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate                                         /* merge URL and supplied filters */
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate                                         *cp2++ = '(';
7287c478bd9Sstevel@tonic-gate                                         *cp2++ = '&';
7297c478bd9Sstevel@tonic-gate                                         /* copy URL filter */
7307c478bd9Sstevel@tonic-gate                                         while (*cp) {
7317c478bd9Sstevel@tonic-gate                                                 *cp2++ = *cp++;
7327c478bd9Sstevel@tonic-gate                                         }
7337c478bd9Sstevel@tonic-gate                                         /* append supplied filter */
7347c478bd9Sstevel@tonic-gate                                         while (*filter) {
7357c478bd9Sstevel@tonic-gate                                                 *cp2++ = *filter++;
7367c478bd9Sstevel@tonic-gate                                         }
7377c478bd9Sstevel@tonic-gate                                         *cp2++ = ')';
7387c478bd9Sstevel@tonic-gate                                 } else {
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate                                         /* copy URL filter */
7417c478bd9Sstevel@tonic-gate                                         while (*cp) {
7427c478bd9Sstevel@tonic-gate                                                 *cp2++ = *cp++;
7437c478bd9Sstevel@tonic-gate                                         }
7447c478bd9Sstevel@tonic-gate                                 }
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate                         } else {
7477c478bd9Sstevel@tonic-gate                                 /* append supplied filter */
7487c478bd9Sstevel@tonic-gate                                 if (filter) {
7497c478bd9Sstevel@tonic-gate                                         if (no_scope) {
7507c478bd9Sstevel@tonic-gate                                                 if (no_attrs) {
7517c478bd9Sstevel@tonic-gate                                                         *cp2++ = '?';
7527c478bd9Sstevel@tonic-gate                                                 }
7537c478bd9Sstevel@tonic-gate                                                 *cp2++ = '?';
7547c478bd9Sstevel@tonic-gate                                         }
7557c478bd9Sstevel@tonic-gate                                         *cp2++ = '?';
7567c478bd9Sstevel@tonic-gate                                         while (*filter) {
7577c478bd9Sstevel@tonic-gate                                                 *cp2++ = *filter++;
7587c478bd9Sstevel@tonic-gate                                         }
7597c478bd9Sstevel@tonic-gate                                 }
7607c478bd9Sstevel@tonic-gate                         }
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate                         *cp2++ = '\0';
7637c478bd9Sstevel@tonic-gate                         free (url);
7647c478bd9Sstevel@tonic-gate                         url = url2;
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate                 } else {
7677c478bd9Sstevel@tonic-gate                         return (0);     /* not an LDAP URL */
7687c478bd9Sstevel@tonic-gate                 }
7697c478bd9Sstevel@tonic-gate         }
7707c478bd9Sstevel@tonic-gate         return (url);
7717c478bd9Sstevel@tonic-gate }
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate /*
7747c478bd9Sstevel@tonic-gate  * Locate the LDAP URL associated with a distinguished name.
7757c478bd9Sstevel@tonic-gate  *
7767c478bd9Sstevel@tonic-gate  * The number of nameparts in the supplied distinguished name must be
7777c478bd9Sstevel@tonic-gate  * provided. The specified directory entry is searched for a labeledURI
7787c478bd9Sstevel@tonic-gate  * attribute. If successful then the LDAP URL is returned. If unsuccessful
7797c478bd9Sstevel@tonic-gate  * then that entry's parent is searched and so on until the target
7807c478bd9Sstevel@tonic-gate  * distinguished name is reduced to only two nameparts.
7817c478bd9Sstevel@tonic-gate  *
7827c478bd9Sstevel@tonic-gate  * For example, if 'l=ny,ou=eng,o=wiz,c=us' is the distinguished name
7837c478bd9Sstevel@tonic-gate  * then the following entries are searched until one succeeds:
7847c478bd9Sstevel@tonic-gate  *              l=ny,ou=eng,o=wiz,c=us
7857c478bd9Sstevel@tonic-gate  *              ou=eng,o=wiz,c=us
7867c478bd9Sstevel@tonic-gate  *              o=wiz,c=us
7877c478bd9Sstevel@tonic-gate  *
7887c478bd9Sstevel@tonic-gate  * If an error is encountered then zero is returned, otherwise a string
7897c478bd9Sstevel@tonic-gate  * URL is returned. The caller should free the returned string if it is
7907c478bd9Sstevel@tonic-gate  * non-zero.
7917c478bd9Sstevel@tonic-gate  */
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate char *
ldap_dn_to_url(LDAP * ld,char * dn,int nameparts)7947c478bd9Sstevel@tonic-gate ldap_dn_to_url(
7957c478bd9Sstevel@tonic-gate         LDAP    *ld,
7967c478bd9Sstevel@tonic-gate         char    *dn,
7977c478bd9Sstevel@tonic-gate         int     nameparts
7987c478bd9Sstevel@tonic-gate )
7997c478bd9Sstevel@tonic-gate {
8007c478bd9Sstevel@tonic-gate         char            *next_dn = dn;
8017c478bd9Sstevel@tonic-gate         char            *url = 0;
8027c478bd9Sstevel@tonic-gate         char            *attrs[2] = {"labeledURI", 0};
8037c478bd9Sstevel@tonic-gate         LDAPMessage     *res, *e;
8047c478bd9Sstevel@tonic-gate         char            **vals;
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate         /*
8077c478bd9Sstevel@tonic-gate          * Search for a URL in the named entry or its parent entry.
8087c478bd9Sstevel@tonic-gate          * Continue until only 2 nameparts remain.
8097c478bd9Sstevel@tonic-gate          */
8107c478bd9Sstevel@tonic-gate         while (dn && (nameparts > 1) && (! url)) {
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate                 /* search for the labeledURI attribute */
8137c478bd9Sstevel@tonic-gate                 if (ldap_search_s(ld, dn, LDAP_SCOPE_BASE,
8147c478bd9Sstevel@tonic-gate                     "(objectClass=*)", attrs, 0, &res) == LDAP_SUCCESS) {
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate                         /* locate the first entry returned */
8177c478bd9Sstevel@tonic-gate                         if ((e = ldap_first_entry(ld, res)) != NULL) {
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate                                 /* locate the labeledURI attribute */
8207c478bd9Sstevel@tonic-gate                                 if ((vals =
8217c478bd9Sstevel@tonic-gate                                     ldap_get_values(ld, e, "labeledURI")) !=
8227c478bd9Sstevel@tonic-gate                                     NULL) {
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate                                         /* copy the attribute value */
8257c478bd9Sstevel@tonic-gate                                         if ((url = strdup((char *)vals[0])) !=
8267c478bd9Sstevel@tonic-gate                                             NULL) {
8277c478bd9Sstevel@tonic-gate                                                 ldap_value_free(vals);
8287c478bd9Sstevel@tonic-gate                                         }
8297c478bd9Sstevel@tonic-gate                                 }
8307c478bd9Sstevel@tonic-gate                         }
8317c478bd9Sstevel@tonic-gate                         /* free the search results */
8327c478bd9Sstevel@tonic-gate 			if (res != NULL) {
8337c478bd9Sstevel@tonic-gate                         	ldap_msgfree(res);
8347c478bd9Sstevel@tonic-gate 			}
8357c478bd9Sstevel@tonic-gate                 }
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate                 if (! url) {
8387c478bd9Sstevel@tonic-gate                         /* advance along the DN by one namepart */
8397c478bd9Sstevel@tonic-gate                         if (next_dn = strchr(dn, ',')) {
8407c478bd9Sstevel@tonic-gate                                 next_dn++;
8417c478bd9Sstevel@tonic-gate                                 dn = next_dn;
8427c478bd9Sstevel@tonic-gate                                 nameparts--;
8437c478bd9Sstevel@tonic-gate                         }
8447c478bd9Sstevel@tonic-gate                 }
8457c478bd9Sstevel@tonic-gate         }
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate         return (url);
8487c478bd9Sstevel@tonic-gate }
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate #endif /* _SOLARIS_SDK */
851