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 * 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 1558
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((
43 static void hex_unescape LDAP_P(( char *s ));
44 static int unhex( char c );
48 ldap_is_ldap_url( LDAP_CONST char *url )
57 if( skip_url_prefix( url, &enclosed, &ldaps) == NULL ) {
65 ldap_is_ldaps_url( LDAP_CONST char *url )
74 if( skip_url_prefix( url, &enclosed, &ldaps) == NULL ) {
88 * return non-zero if this looks like a LDAP URL; zero if not
89 * if non-zero returned, *urlp will be moved past "ldap://" part of URL
99 /* skip leading '<' (if any) */
107 /* skip leading "URL:" (if any) */
108 if ( strncasecmp( p, LDAP_URL_URLCOLON, LDAP_URL_URLCOLON_LEN ) == 0 )
110 p += LDAP_URL_URLCOLON_LEN;
113 /* check for "ldap://" prefix */
114 if ( strncasecmp( p, LDAP_URL_PREFIX, LDAP_URL_PREFIX_LEN ) == 0 ) {
115 /* skip over "ldap://" prefix and return success */
116 p += LDAP_URL_PREFIX_LEN;
121 /* check for "ldaps://" prefix */
122 if ( strncasecmp( p, LDAPS_URL_PREFIX, LDAPS_URL_PREFIX_LEN ) == 0 ) {
123 /* skip over "ldaps://" prefix and return success */
124 p += LDAPS_URL_PREFIX_LEN;
133 static int str2scope( const char *p )
135 if ( strcasecmp( p, "one" ) == 0 ) {
136 return LDAP_SCOPE_ONELEVEL;
138 } else if ( strcasecmp( p, "onetree" ) == 0 ) {
139 return LDAP_SCOPE_ONELEVEL;
141 } else if ( strcasecmp( p, "base" ) == 0 ) {
142 return LDAP_SCOPE_BASE;
144 } else if ( strcasecmp( p, "sub" ) == 0 ) {
145 return LDAP_SCOPE_SUBTREE;
147 } else if ( strcasecmp( p, "subtree" ) == 0 ) {
148 return LDAP_SCOPE_SUBTREE;
156 ldap_url_parse( LDAP_CONST char *url_in, LDAPURLDesc **ludpp )
159 * Pick apart the pieces of an LDAP URL.
164 int i, enclosed, ldaps;
168 if( url_in == NULL && ludpp == NULL ) {
169 return LDAP_URL_ERR_PARAM;
172 Debug( LDAP_DEBUG_TRACE, "ldap_url_parse(%s)\n", url_in, 0, 0 );
174 *ludpp = NULL; /* pessimistic */
176 url_tmp = skip_url_prefix( url_in, &enclosed, &ldaps );
178 if ( url_tmp == NULL ) {
179 return LDAP_URL_ERR_NOTLDAP;
182 /* make working copy of the remainder of the URL */
183 if (( url = LDAP_STRDUP( url_tmp )) == NULL ) {
184 return( LDAP_URL_ERR_MEM );
188 p = &url[strlen(url)-1];
192 return LDAP_URL_ERR_BADENCLOSURE;
198 /* allocate return struct */
199 ludp = (LDAPURLDesc *)LDAP_CALLOC( 1, sizeof( LDAPURLDesc ));
201 if ( ludp == NULL ) {
203 return LDAP_URL_ERR_MEM;
206 ludp->lud_host = NULL;
209 ludp->lud_attrs = NULL;
210 ludp->lud_filter = NULL;
211 ludp->lud_ldaps = ldaps;
212 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 ) {
235 ldap_free_urldesc( ludp );
236 return LDAP_URL_ERR_BADURL;
239 ludp->lud_port = atoi( q );
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 */
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 */
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 */
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 */
358 ldap_free_urldesc( ludp );
359 return LDAP_URL_ERR_BADFILTER;
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 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 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 = unhex( *s ) << 4;
559 if ( *++s != '\0' ) {
574 return( c >= '0' && c <= '9' ? c - '0'
575 : c >= 'A' && c <= 'F' ? c - 'A' + 10