3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1996 Regents of the University of Michigan.
10 * LIBLDAP url.c -- LDAP URL (RFC 2255) related routines
12 * LDAP URLs look like this:
13 * ldap[s]://host:port[/[dn[?[attributes][?[scope][?[filter][?exts]]]]]]
16 * attributes is a comma separated list
17 * scope is one of these three strings: base one sub (default=base)
18 * filter is an string-represented filter as in RFC 2254
20 * e.g., ldap://host:port/dc=com?o,cn?base?o=openldap?extension
22 * We also tolerate URLs that look like: <ldapurl> and <URL:ldapurl>
29 #include <ac/stdlib.h>
32 #include <ac/socket.h>
33 #include <ac/string.h>
40 static const char* skip_url_prefix LDAP_P((
47 ldap_is_ldap_url( LDAP_CONST char *url )
56 if( skip_url_prefix( url, &enclosed, &ldaps) == NULL ) {
64 ldap_is_ldaps_url( LDAP_CONST char *url )
73 if( skip_url_prefix( url, &enclosed, &ldaps) == NULL ) {
87 * return non-zero if this looks like a LDAP URL; zero if not
88 * if non-zero returned, *urlp will be moved past "ldap://" part of URL
98 /* skip leading '<' (if any) */
106 /* skip leading "URL:" (if any) */
107 if ( strncasecmp( p, LDAP_URL_URLCOLON, LDAP_URL_URLCOLON_LEN ) == 0 )
109 p += LDAP_URL_URLCOLON_LEN;
112 /* check for "ldap://" prefix */
113 if ( strncasecmp( p, LDAP_URL_PREFIX, LDAP_URL_PREFIX_LEN ) == 0 ) {
114 /* skip over "ldap://" prefix and return success */
115 p += LDAP_URL_PREFIX_LEN;
120 /* check for "ldaps://" prefix */
121 if ( strncasecmp( p, LDAPS_URL_PREFIX, LDAPS_URL_PREFIX_LEN ) == 0 ) {
122 /* skip over "ldaps://" prefix and return success */
123 p += LDAPS_URL_PREFIX_LEN;
132 static int str2scope( const char *p )
134 if ( strcasecmp( p, "one" ) == 0 ) {
135 return LDAP_SCOPE_ONELEVEL;
137 } else if ( strcasecmp( p, "onetree" ) == 0 ) {
138 return LDAP_SCOPE_ONELEVEL;
140 } else if ( strcasecmp( p, "base" ) == 0 ) {
141 return LDAP_SCOPE_BASE;
143 } else if ( strcasecmp( p, "sub" ) == 0 ) {
144 return LDAP_SCOPE_SUBTREE;
146 } else if ( strcasecmp( p, "subtree" ) == 0 ) {
147 return LDAP_SCOPE_SUBTREE;
155 ldap_url_parse( LDAP_CONST char *url_in, LDAPURLDesc **ludpp )
158 * Pick apart the pieces of an LDAP URL.
163 int i, enclosed, ldaps;
167 if( url_in == NULL && ludpp == NULL ) {
168 return LDAP_URL_ERR_PARAM;
171 Debug( LDAP_DEBUG_TRACE, "ldap_url_parse(%s)\n", url_in, 0, 0 );
173 *ludpp = NULL; /* pessimistic */
175 url_tmp = skip_url_prefix( url_in, &enclosed, &ldaps );
177 if ( url_tmp == NULL ) {
178 return LDAP_URL_ERR_NOTLDAP;
181 /* make working copy of the remainder of the URL */
182 if (( url = LDAP_STRDUP( url_tmp )) == NULL ) {
183 return( LDAP_URL_ERR_MEM );
187 p = &url[strlen(url)-1];
191 return LDAP_URL_ERR_BADENCLOSURE;
197 /* allocate return struct */
198 ludp = (LDAPURLDesc *)LDAP_CALLOC( 1, sizeof( LDAPURLDesc ));
200 if ( ludp == NULL ) {
202 return LDAP_URL_ERR_MEM;
205 ludp->lud_host = NULL;
208 ludp->lud_attrs = NULL;
209 ludp->lud_filter = NULL;
210 ludp->lud_ldaps = ldaps;
211 ludp->lud_scope = LDAP_SCOPE_BASE;
213 ludp->lud_filter = LDAP_STRDUP("(objectClass=*)");
215 if( ludp->lud_filter == NULL ) {
217 ldap_free_urldesc( ludp );
218 return LDAP_URL_ERR_MEM;
221 /* scan forward for '/' that marks end of hostport and begin. of dn */
222 p = strchr( url, '/' );
225 /* terminate hostport; point to start of dn */
229 if (( q = strchr( url, ':' )) != NULL ) {
231 ldap_pvt_hex_unescape( q );
235 ldap_free_urldesc( ludp );
236 return LDAP_URL_ERR_BADURL;
239 ludp->lud_port = atoi( q );
242 ldap_pvt_hex_unescape( url );
243 ludp->lud_host = LDAP_STRDUP( url );
245 if( ludp->lud_host == NULL ) {
247 ldap_free_urldesc( ludp );
248 return LDAP_URL_ERR_MEM;
254 return LDAP_URL_SUCCESS;
257 /* scan forward for '?' that may marks end of dn */
258 q = strchr( p, '?' );
261 /* terminate dn part */
267 ldap_pvt_hex_unescape( p );
268 ludp->lud_dn = LDAP_STRDUP( p );
270 ludp->lud_dn = LDAP_STRDUP( "" );
273 if( ludp->lud_dn == NULL ) {
275 ldap_free_urldesc( ludp );
276 return LDAP_URL_ERR_MEM;
283 return LDAP_URL_SUCCESS;
286 /* scan forward for '?' that may marks end of attributes */
288 q = strchr( p, '?' );
291 /* terminate attributes part */
296 /* parse attributes */
297 ldap_pvt_hex_unescape( p );
298 ludp->lud_attrs = ldap_str2charray( p, "," );
300 if( ludp->lud_attrs == NULL ) {
302 ldap_free_urldesc( ludp );
303 return LDAP_URL_ERR_BADATTRS;
311 return LDAP_URL_SUCCESS;
314 /* scan forward for '?' that may marks end of scope */
316 q = strchr( p, '?' );
319 /* terminate the scope part */
324 /* parse the scope */
325 ldap_pvt_hex_unescape( p );
326 ludp->lud_scope = str2scope( p );
328 if( ludp->lud_scope == -1 ) {
330 ldap_free_urldesc( ludp );
331 return LDAP_URL_ERR_BADSCOPE;
339 return LDAP_URL_SUCCESS;
342 /* scan forward for '?' that may marks end of filter */
344 q = strchr( p, '?' );
347 /* terminate the filter part */
352 /* parse the filter */
353 ldap_pvt_hex_unescape( p );
358 ldap_free_urldesc( ludp );
359 return LDAP_URL_ERR_BADFILTER;
362 LDAP_FREE( ludp->lud_filter );
363 ludp->lud_filter = LDAP_STRDUP( p );
365 if( ludp->lud_filter == NULL ) {
367 ldap_free_urldesc( ludp );
368 return LDAP_URL_ERR_MEM;
376 return LDAP_URL_SUCCESS;
379 /* scan forward for '?' that may marks end of extensions */
381 q = strchr( p, '?' );
386 ldap_free_urldesc( ludp );
387 return LDAP_URL_ERR_BADURL;
390 /* parse the extensions */
391 ludp->lud_exts = ldap_str2charray( p, "," );
393 if( ludp->lud_exts == NULL ) {
395 ldap_free_urldesc( ludp );
396 return LDAP_URL_ERR_BADEXTS;
399 for( i=0; ludp->lud_exts[i] != NULL; i++ ) {
400 ldap_pvt_hex_unescape( ludp->lud_exts[i] );
404 /* must have 1 or more */
405 ldap_charray_free( ludp->lud_exts );
407 ldap_free_urldesc( ludp );
408 return LDAP_URL_ERR_BADEXTS;
414 return LDAP_URL_SUCCESS;
419 ldap_free_urldesc( LDAPURLDesc *ludp )
421 if ( ludp == NULL ) {
425 if ( ludp->lud_host != NULL ) {
426 LDAP_FREE( ludp->lud_host );
429 if ( ludp->lud_dn != NULL ) {
430 LDAP_FREE( ludp->lud_dn );
433 if ( ludp->lud_filter != NULL ) {
434 LDAP_FREE( ludp->lud_filter);
437 if ( ludp->lud_attrs != NULL ) {
438 LDAP_VFREE( ludp->lud_attrs );
441 if ( ludp->lud_exts != NULL ) {
442 LDAP_VFREE( ludp->lud_exts );
451 ldap_url_search( LDAP *ld, LDAP_CONST char *url, int attrsonly )
456 LDAPServer *srv = NULL;
458 if ( ldap_url_parse( url, &ludp ) != 0 ) {
459 ld->ld_errno = LDAP_PARAM_ERROR;
463 ber = ldap_build_search_req( ld, ludp->lud_dn, ludp->lud_scope,
464 ludp->lud_filter, ludp->lud_attrs, attrsonly, NULL, NULL,
473 if ( ludp->lud_host != NULL || ludp->lud_port != 0 ) {
474 if (( srv = (LDAPServer *)LDAP_CALLOC( 1, sizeof( LDAPServer )))
475 == NULL || ( srv->lsrv_host = LDAP_STRDUP( ludp->lud_host ==
476 NULL ? ld->ld_defhost : ludp->lud_host )) == NULL ) {
480 ld->ld_errno = LDAP_NO_MEMORY;
483 if ( ludp->lud_port == 0 ) {
484 srv->lsrv_port = ldap_int_global_options.ldo_defport;
486 srv->lsrv_port = ludp->lud_port;
494 err = ldap_send_server_request( ld, ber, ld->ld_msgid, NULL, srv,
498 ldap_free_urldesc( ludp );
505 ldap_url_search_st( LDAP *ld, LDAP_CONST char *url, int attrsonly,
506 struct timeval *timeout, LDAPMessage **res )
510 if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
511 return( ld->ld_errno );
514 if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 ) {
515 return( ld->ld_errno );
518 if ( ld->ld_errno == LDAP_TIMEOUT ) {
519 (void) ldap_abandon( ld, msgid );
520 ld->ld_errno = LDAP_TIMEOUT;
521 return( ld->ld_errno );
524 return( ldap_result2error( ld, *res, 0 ));
530 LDAP *ld, LDAP_CONST char *url, int attrsonly, LDAPMessage **res )
534 if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
535 return( ld->ld_errno );
538 if ( ldap_result( ld, msgid, 1, (struct timeval *)NULL, res ) == -1 ) {
539 return( ld->ld_errno );
542 return( ldap_result2error( ld, *res, 0 ));
547 ldap_pvt_hex_unescape( char *s )
550 * Remove URL hex escapes from s... done in place. The basic concept for
551 * this routine is borrowed from the WWW library HTUnEscape() routine.
555 for ( p = s; *s != '\0'; ++s ) {
557 if ( *++s != '\0' ) {
558 *p = ldap_pvt_unhex( *s ) << 4;
560 if ( *++s != '\0' ) {
561 *p++ += ldap_pvt_unhex( *s );
573 ldap_pvt_unhex( int c )
575 return( c >= '0' && c <= '9' ? c - '0'
576 : c >= 'A' && c <= 'F' ? c - 'A' + 10