2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 * Copyright (c) 1996 Regents of the University of Michigan.
9 * LIBLDAP url.c -- LDAP URL related routines
11 * LDAP URLs look like this:
12 * l d a p : / / hostport / dn [ ? attributes [ ? scope [ ? filter ] ] ]
15 * attributes is a comma separated list
16 * scope is one of these three strings: base one sub (default=base)
17 * filter is an string-represented filter as in RFC 1558
19 * e.g., ldap://ldap.itd.umich.edu/c=US?o,description?one?o=umich
21 * We also tolerate URLs that look like: <ldapurl> and <URL:ldapurl>
28 #include <ac/stdlib.h>
31 #include <ac/socket.h>
32 #include <ac/string.h>
39 static const char* skip_url_prefix LDAP_P(( const char *url, int *enclosedp ));
40 static void hex_unescape LDAP_P(( char *s ));
41 static int unhex( char c );
45 ldap_is_ldap_url( LDAP_CONST char *url )
49 return( url != NULL && skip_url_prefix( url, &enclosed ) != NULL );
54 skip_url_prefix( const char *url, int *enclosedp )
57 * return non-zero if this looks like a LDAP URL; zero if not
58 * if non-zero returned, *urlp will be moved past "ldap://" part of URL
68 /* skip leading '<' (if any) */
76 /* skip leading "URL:" (if any) */
77 if ( strlen( p ) >= LDAP_URL_URLCOLON_LEN
78 && strncasecmp( p, LDAP_URL_URLCOLON, LDAP_URL_URLCOLON_LEN ) == 0 )
80 p += LDAP_URL_URLCOLON_LEN;
83 /* check for missing "ldap://" prefix */
84 if ( strlen( p ) < LDAP_URL_PREFIX_LEN ||
85 strncasecmp( p, LDAP_URL_PREFIX, LDAP_URL_PREFIX_LEN ) != 0 ) {
89 /* skip over "ldap://" prefix and return success */
90 p += LDAP_URL_PREFIX_LEN;
97 ldap_url_parse( LDAP_CONST char *url_in, LDAPURLDesc **ludpp )
100 * Pick apart the pieces of an LDAP URL.
105 int enclosed, i, nattrs;
109 Debug( LDAP_DEBUG_TRACE, "ldap_url_parse(%s)\n", url_in, 0, 0 );
111 *ludpp = NULL; /* pessimistic */
113 url_tmp = skip_url_prefix( url_in, &enclosed );
115 if ( url_tmp == NULL ) {
116 return( LDAP_URL_ERR_NOTLDAP );
119 /* make working copy of the remainder of the URL */
120 if (( url = LDAP_STRDUP( url_tmp )) == NULL ) {
121 return( LDAP_URL_ERR_MEM );
124 /* allocate return struct */
125 if (( ludp = (LDAPURLDesc *)LDAP_CALLOC( 1, sizeof( LDAPURLDesc )))
129 return( LDAP_URL_ERR_MEM );
133 if ( enclosed && *((p = url + strlen( url ) - 1)) == '>' ) {
138 ludp->lud_scope = LDAP_SCOPE_BASE;
139 ludp->lud_filter = "(objectClass=*)";
141 /* lud_string is the only malloc'd string space we use */
142 ludp->lud_string = url;
144 /* scan forward for '/' that marks end of hostport and begin. of dn */
145 if (( ludp->lud_dn = strchr( url, '/' )) == NULL ) {
146 ldap_free_urldesc( ludp );
147 return( LDAP_URL_ERR_NODN );
150 /* terminate hostport; point to start of dn */
151 *ludp->lud_dn++ = '\0';
153 if (( p = strchr( url, ':' )) != NULL ) {
155 ludp->lud_port = atoi( p );
158 if ( *url == '\0' ) {
159 ludp->lud_host = NULL;
161 ludp->lud_host = url;
162 hex_unescape( ludp->lud_host );
165 /* scan for '?' that marks end of dn and beginning of attributes */
166 if (( attrs = strchr( ludp->lud_dn, '?' )) != NULL ) {
167 /* terminate dn; point to start of attrs. */
170 /* scan for '?' that marks end of attrs and begin. of scope */
171 if (( p = strchr( attrs, '?' )) != NULL ) {
173 * terminate attrs; point to start of scope and scan for
174 * '?' that marks end of scope and begin. of filter
178 if (( q = strchr( p, '?' )) != NULL ) {
179 /* terminate scope; point to start of filter */
182 ludp->lud_filter = q;
183 hex_unescape( ludp->lud_filter );
187 if ( strcasecmp( p, "one" ) == 0 ) {
188 ludp->lud_scope = LDAP_SCOPE_ONELEVEL;
189 } else if ( strcasecmp( p, "base" ) == 0 ) {
190 ludp->lud_scope = LDAP_SCOPE_BASE;
191 } else if ( strcasecmp( p, "sub" ) == 0 ) {
192 ludp->lud_scope = LDAP_SCOPE_SUBTREE;
193 } else if ( *p != '\0' ) {
194 ldap_free_urldesc( ludp );
195 return( LDAP_URL_ERR_BADSCOPE );
200 if ( *ludp->lud_dn == '\0' ) {
203 hex_unescape( ludp->lud_dn );
207 * if attrs list was included, turn it into a null-terminated array
209 if ( attrs != NULL && *attrs != '\0' ) {
210 for ( nattrs = 1, p = attrs; *p != '\0'; ++p ) {
216 if (( ludp->lud_attrs = (char **)LDAP_CALLOC( nattrs + 1,
217 sizeof( char * ))) == NULL ) {
218 ldap_free_urldesc( ludp );
219 return( LDAP_URL_ERR_MEM );
222 for ( i = 0, p = attrs; i < nattrs; ++i ) {
223 ludp->lud_attrs[ i ] = p;
224 if (( p = strchr( p, ',' )) != NULL ) {
227 hex_unescape( ludp->lud_attrs[ i ] );
238 ldap_free_urldesc( LDAPURLDesc *ludp )
240 if ( ludp != NULLLDAPURLDESC ) {
241 if ( ludp->lud_string != NULL ) {
242 LDAP_FREE( ludp->lud_string );
244 if ( ludp->lud_attrs != NULL ) {
245 LDAP_FREE( ludp->lud_attrs );
254 ldap_url_search( LDAP *ld, LDAP_CONST char *url, int attrsonly )
259 LDAPServer *srv = NULL;
261 if ( ldap_url_parse( url, &ludp ) != 0 ) {
262 ld->ld_errno = LDAP_PARAM_ERROR;
266 ber = ldap_build_search_req( ld, ludp->lud_dn, ludp->lud_scope,
267 ludp->lud_filter, ludp->lud_attrs, attrsonly, NULL, NULL,
270 if ( ber == NULLBER ) {
276 if ( ludp->lud_host != NULL || ludp->lud_port != 0 ) {
277 if (( srv = (LDAPServer *)LDAP_CALLOC( 1, sizeof( LDAPServer )))
278 == NULL || ( srv->lsrv_host = LDAP_STRDUP( ludp->lud_host ==
279 NULL ? ld->ld_defhost : ludp->lud_host )) == NULL ) {
283 ld->ld_errno = LDAP_NO_MEMORY;
286 if ( ludp->lud_port == 0 ) {
287 srv->lsrv_port = ldap_int_global_options.ldo_defport;
289 srv->lsrv_port = ludp->lud_port;
297 err = ldap_send_server_request( ld, ber, ld->ld_msgid, NULL, srv,
301 ldap_free_urldesc( ludp );
308 ldap_url_search_st( LDAP *ld, LDAP_CONST char *url, int attrsonly,
309 struct timeval *timeout, LDAPMessage **res )
313 if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
314 return( ld->ld_errno );
317 if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 ) {
318 return( ld->ld_errno );
321 if ( ld->ld_errno == LDAP_TIMEOUT ) {
322 (void) ldap_abandon( ld, msgid );
323 ld->ld_errno = LDAP_TIMEOUT;
324 return( ld->ld_errno );
327 return( ldap_result2error( ld, *res, 0 ));
333 LDAP *ld, LDAP_CONST char *url, int attrsonly, LDAPMessage **res )
337 if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
338 return( ld->ld_errno );
341 if ( ldap_result( ld, msgid, 1, (struct timeval *)NULL, res ) == -1 ) {
342 return( ld->ld_errno );
345 return( ldap_result2error( ld, *res, 0 ));
350 hex_unescape( char *s )
353 * Remove URL hex escapes from s... done in place. The basic concept for
354 * this routine is borrowed from the WWW library HTUnEscape() routine.
358 for ( p = s; *s != '\0'; ++s ) {
360 if ( *++s != '\0' ) {
361 *p = unhex( *s ) << 4;
363 if ( *++s != '\0' ) {
378 return( c >= '0' && c <= '9' ? c - '0'
379 : c >= 'A' && c <= 'F' ? c - 'A' + 10