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 (RFC 2255) related routines
11 * LDAP URLs look like this:
12 * ldap[s]://host:port[/[dn[?[attributes][?[scope][?[filter][?exts]]]]]]
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 2254
19 * e.g., ldap://host:port/dc=com?o,cn?base?o=openldap?extension
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((
46 ldap_is_ldap_url( LDAP_CONST char *url )
55 if( skip_url_prefix( url, &enclosed, &ldaps) == NULL ) {
63 ldap_is_ldaps_url( LDAP_CONST char *url )
72 if( skip_url_prefix( url, &enclosed, &ldaps) == NULL ) {
86 * return non-zero if this looks like a LDAP URL; zero if not
87 * if non-zero returned, *urlp will be moved past "ldap://" part of URL
97 /* skip leading '<' (if any) */
105 /* skip leading "URL:" (if any) */
106 if ( strncasecmp( p, LDAP_URL_URLCOLON, LDAP_URL_URLCOLON_LEN ) == 0 )
108 p += LDAP_URL_URLCOLON_LEN;
111 /* check for "ldap://" prefix */
112 if ( strncasecmp( p, LDAP_URL_PREFIX, LDAP_URL_PREFIX_LEN ) == 0 ) {
113 /* skip over "ldap://" prefix and return success */
114 p += LDAP_URL_PREFIX_LEN;
119 /* check for "ldaps://" prefix */
120 if ( strncasecmp( p, LDAPS_URL_PREFIX, LDAPS_URL_PREFIX_LEN ) == 0 ) {
121 /* skip over "ldaps://" prefix and return success */
122 p += LDAPS_URL_PREFIX_LEN;
131 static int str2scope( const char *p )
133 if ( strcasecmp( p, "one" ) == 0 ) {
134 return LDAP_SCOPE_ONELEVEL;
136 } else if ( strcasecmp( p, "onetree" ) == 0 ) {
137 return LDAP_SCOPE_ONELEVEL;
139 } else if ( strcasecmp( p, "base" ) == 0 ) {
140 return LDAP_SCOPE_BASE;
142 } else if ( strcasecmp( p, "sub" ) == 0 ) {
143 return LDAP_SCOPE_SUBTREE;
145 } else if ( strcasecmp( p, "subtree" ) == 0 ) {
146 return LDAP_SCOPE_SUBTREE;
154 ldap_url_parse( LDAP_CONST char *url_in, LDAPURLDesc **ludpp )
157 * Pick apart the pieces of an LDAP URL.
162 int i, enclosed, ldaps;
166 if( url_in == NULL && ludpp == NULL ) {
167 return LDAP_URL_ERR_PARAM;
170 Debug( LDAP_DEBUG_TRACE, "ldap_url_parse(%s)\n", url_in, 0, 0 );
172 *ludpp = NULL; /* pessimistic */
174 url_tmp = skip_url_prefix( url_in, &enclosed, &ldaps );
176 if ( url_tmp == NULL ) {
177 return LDAP_URL_ERR_NOTLDAP;
180 /* make working copy of the remainder of the URL */
181 if (( url = LDAP_STRDUP( url_tmp )) == NULL ) {
182 return( LDAP_URL_ERR_MEM );
186 p = &url[strlen(url)-1];
190 return LDAP_URL_ERR_BADENCLOSURE;
196 /* allocate return struct */
197 ludp = (LDAPURLDesc *)LDAP_CALLOC( 1, sizeof( LDAPURLDesc ));
199 if ( ludp == NULL ) {
201 return LDAP_URL_ERR_MEM;
204 ludp->lud_host = NULL;
207 ludp->lud_attrs = NULL;
208 ludp->lud_filter = NULL;
209 ludp->lud_ldaps = ldaps;
210 ludp->lud_scope = LDAP_SCOPE_BASE;
212 ludp->lud_filter = LDAP_STRDUP("(objectClass=*)");
214 if( ludp->lud_filter == NULL ) {
216 ldap_free_urldesc( ludp );
217 return LDAP_URL_ERR_MEM;
220 /* scan forward for '/' that marks end of hostport and begin. of dn */
221 p = strchr( url, '/' );
224 /* terminate hostport; point to start of dn */
228 if (( q = strchr( url, ':' )) != NULL ) {
230 ldap_pvt_hex_unescape( q );
234 ldap_free_urldesc( ludp );
235 return LDAP_URL_ERR_BADURL;
238 ludp->lud_port = atoi( q );
241 ldap_pvt_hex_unescape( url );
242 ludp->lud_host = LDAP_STRDUP( url );
244 if( ludp->lud_host == NULL ) {
246 ldap_free_urldesc( ludp );
247 return LDAP_URL_ERR_MEM;
253 return LDAP_URL_SUCCESS;
256 /* scan forward for '?' that may marks end of dn */
257 q = strchr( p, '?' );
260 /* terminate dn part */
266 ldap_pvt_hex_unescape( p );
267 ludp->lud_dn = LDAP_STRDUP( p );
269 ludp->lud_dn = LDAP_STRDUP( "" );
272 if( ludp->lud_dn == NULL ) {
274 ldap_free_urldesc( ludp );
275 return LDAP_URL_ERR_MEM;
282 return LDAP_URL_SUCCESS;
285 /* scan forward for '?' that may marks end of attributes */
287 q = strchr( p, '?' );
290 /* terminate attributes part */
295 /* parse attributes */
296 ldap_pvt_hex_unescape( p );
297 ludp->lud_attrs = ldap_str2charray( p, "," );
299 if( ludp->lud_attrs == NULL ) {
301 ldap_free_urldesc( ludp );
302 return LDAP_URL_ERR_BADATTRS;
310 return LDAP_URL_SUCCESS;
313 /* scan forward for '?' that may marks end of scope */
315 q = strchr( p, '?' );
318 /* terminate the scope part */
323 /* parse the scope */
324 ldap_pvt_hex_unescape( p );
325 ludp->lud_scope = str2scope( p );
327 if( ludp->lud_scope == -1 ) {
329 ldap_free_urldesc( ludp );
330 return LDAP_URL_ERR_BADSCOPE;
338 return LDAP_URL_SUCCESS;
341 /* scan forward for '?' that may marks end of filter */
343 q = strchr( p, '?' );
346 /* terminate the filter part */
351 /* parse the filter */
352 ldap_pvt_hex_unescape( p );
357 ldap_free_urldesc( ludp );
358 return LDAP_URL_ERR_BADFILTER;
361 LDAP_FREE( ludp->lud_filter );
362 ludp->lud_filter = LDAP_STRDUP( p );
364 if( ludp->lud_filter == NULL ) {
366 ldap_free_urldesc( ludp );
367 return LDAP_URL_ERR_MEM;
375 return LDAP_URL_SUCCESS;
378 /* scan forward for '?' that may marks end of extensions */
380 q = strchr( p, '?' );
385 ldap_free_urldesc( ludp );
386 return LDAP_URL_ERR_BADURL;
389 /* parse the extensions */
390 ludp->lud_exts = ldap_str2charray( p, "," );
392 if( ludp->lud_exts == NULL ) {
394 ldap_free_urldesc( ludp );
395 return LDAP_URL_ERR_BADEXTS;
398 for( i=0; ludp->lud_exts[i] != NULL; i++ ) {
399 ldap_pvt_hex_unescape( ludp->lud_exts[i] );
403 /* must have 1 or more */
404 ldap_charray_free( ludp->lud_exts );
406 ldap_free_urldesc( ludp );
407 return LDAP_URL_ERR_BADEXTS;
413 return LDAP_URL_SUCCESS;
418 ldap_free_urldesc( LDAPURLDesc *ludp )
420 if ( ludp == NULL ) {
424 if ( ludp->lud_host != NULL ) {
425 LDAP_FREE( ludp->lud_host );
428 if ( ludp->lud_dn != NULL ) {
429 LDAP_FREE( ludp->lud_dn );
432 if ( ludp->lud_filter != NULL ) {
433 LDAP_FREE( ludp->lud_filter);
436 if ( ludp->lud_attrs != NULL ) {
437 LDAP_VFREE( ludp->lud_attrs );
440 if ( ludp->lud_exts != NULL ) {
441 LDAP_VFREE( ludp->lud_exts );
450 ldap_url_search( LDAP *ld, LDAP_CONST char *url, int attrsonly )
455 LDAPServer *srv = NULL;
457 if ( ldap_url_parse( url, &ludp ) != 0 ) {
458 ld->ld_errno = LDAP_PARAM_ERROR;
462 ber = ldap_build_search_req( ld, ludp->lud_dn, ludp->lud_scope,
463 ludp->lud_filter, ludp->lud_attrs, attrsonly, NULL, NULL,
472 if ( ludp->lud_host != NULL || ludp->lud_port != 0 ) {
473 if (( srv = (LDAPServer *)LDAP_CALLOC( 1, sizeof( LDAPServer )))
474 == NULL || ( srv->lsrv_host = LDAP_STRDUP( ludp->lud_host ==
475 NULL ? ld->ld_defhost : ludp->lud_host )) == NULL ) {
479 ld->ld_errno = LDAP_NO_MEMORY;
482 if ( ludp->lud_port == 0 ) {
483 srv->lsrv_port = ldap_int_global_options.ldo_defport;
485 srv->lsrv_port = ludp->lud_port;
493 err = ldap_send_server_request( ld, ber, ld->ld_msgid, NULL, srv,
497 ldap_free_urldesc( ludp );
504 ldap_url_search_st( LDAP *ld, LDAP_CONST char *url, int attrsonly,
505 struct timeval *timeout, LDAPMessage **res )
509 if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
510 return( ld->ld_errno );
513 if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 ) {
514 return( ld->ld_errno );
517 if ( ld->ld_errno == LDAP_TIMEOUT ) {
518 (void) ldap_abandon( ld, msgid );
519 ld->ld_errno = LDAP_TIMEOUT;
520 return( ld->ld_errno );
523 return( ldap_result2error( ld, *res, 0 ));
529 LDAP *ld, LDAP_CONST char *url, int attrsonly, LDAPMessage **res )
533 if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
534 return( ld->ld_errno );
537 if ( ldap_result( ld, msgid, 1, (struct timeval *)NULL, res ) == -1 ) {
538 return( ld->ld_errno );
541 return( ldap_result2error( ld, *res, 0 ));
546 ldap_pvt_hex_unescape( char *s )
549 * Remove URL hex escapes from s... done in place. The basic concept for
550 * this routine is borrowed from the WWW library HTUnEscape() routine.
554 for ( p = s; *s != '\0'; ++s ) {
556 if ( *++s != '\0' ) {
557 *p = ldap_pvt_unhex( *s ) << 4;
559 if ( *++s != '\0' ) {
560 *p++ += ldap_pvt_unhex( *s );
572 ldap_pvt_unhex( int c )
574 return( c >= '0' && c <= '9' ? c - '0'
575 : c >= 'A' && c <= 'F' ? c - 'A' + 10