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((
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;
214 ludp->lud_filter = LDAP_STRDUP("(objectClass=*)");
216 if( ludp->lud_filter == NULL ) {
218 ldap_free_urldesc( ludp );
219 return LDAP_URL_ERR_MEM;
222 /* scan forward for '/' that marks end of hostport and begin. of dn */
223 p = strchr( url, '/' );
226 /* terminate hostport; point to start of dn */
230 if (( q = strchr( url, ':' )) != NULL ) {
236 ldap_free_urldesc( ludp );
237 return LDAP_URL_ERR_BADURL;
240 ludp->lud_port = atoi( q );
244 ludp->lud_host = LDAP_STRDUP( url );
246 if( ludp->lud_host == NULL ) {
248 ldap_free_urldesc( ludp );
249 return LDAP_URL_ERR_MEM;
255 return LDAP_URL_SUCCESS;
258 /* scan forward for '?' that may marks end of dn */
259 q = strchr( p, '?' );
262 /* terminate dn part */
269 ludp->lud_dn = LDAP_STRDUP( p );
271 ludp->lud_dn = LDAP_STRDUP( "" );
274 if( ludp->lud_dn == NULL ) {
276 ldap_free_urldesc( ludp );
277 return LDAP_URL_ERR_MEM;
284 return LDAP_URL_SUCCESS;
287 /* scan forward for '?' that may marks end of attributes */
289 q = strchr( p, '?' );
292 /* terminate attributes part */
297 /* parse attributes */
299 ludp->lud_attrs = ldap_str2charray( p, "," );
301 if( ludp->lud_attrs == NULL ) {
303 ldap_free_urldesc( ludp );
304 return LDAP_URL_ERR_BADATTRS;
312 return LDAP_URL_SUCCESS;
315 /* scan forward for '?' that may marks end of scope */
317 q = strchr( p, '?' );
320 /* terminate the scope part */
325 /* parse the scope */
327 ludp->lud_scope = str2scope( p );
329 if( ludp->lud_scope == -1 ) {
331 ldap_free_urldesc( ludp );
332 return LDAP_URL_ERR_BADSCOPE;
340 return LDAP_URL_SUCCESS;
343 /* scan forward for '?' that may marks end of filter */
345 q = strchr( p, '?' );
348 /* terminate the filter part */
353 /* parse the filter */
359 ldap_free_urldesc( ludp );
360 return LDAP_URL_ERR_BADFILTER;
363 LDAP_FREE( ludp->lud_filter );
364 ludp->lud_filter = LDAP_STRDUP( p );
366 if( ludp->lud_filter == NULL ) {
368 ldap_free_urldesc( ludp );
369 return LDAP_URL_ERR_MEM;
377 return LDAP_URL_SUCCESS;
380 /* scan forward for '?' that may marks end of extensions */
382 q = strchr( p, '?' );
387 ldap_free_urldesc( ludp );
388 return LDAP_URL_ERR_BADURL;
391 /* parse the extensions */
392 ludp->lud_exts = ldap_str2charray( p, "," );
394 if( ludp->lud_exts == NULL ) {
396 ldap_free_urldesc( ludp );
397 return LDAP_URL_ERR_BADEXTS;
400 for( i=0; ludp->lud_exts[i] != NULL; i++ ) {
401 hex_unescape( ludp->lud_exts[i] );
405 /* must have 1 or more */
406 ldap_charray_free( ludp->lud_exts );
408 ldap_free_urldesc( ludp );
409 return LDAP_URL_ERR_BADEXTS;
415 return LDAP_URL_SUCCESS;
420 ldap_free_urldesc( LDAPURLDesc *ludp )
422 if ( ludp == NULL ) {
426 if ( ludp->lud_host != NULL ) {
427 LDAP_FREE( ludp->lud_host );
430 if ( ludp->lud_dn != NULL ) {
431 LDAP_FREE( ludp->lud_dn );
434 if ( ludp->lud_filter != NULL ) {
435 LDAP_FREE( ludp->lud_filter);
438 if ( ludp->lud_attrs != NULL ) {
439 LDAP_VFREE( ludp->lud_attrs );
442 if ( ludp->lud_exts != NULL ) {
443 LDAP_VFREE( ludp->lud_exts );
452 ldap_url_search( LDAP *ld, LDAP_CONST char *url, int attrsonly )
457 LDAPServer *srv = NULL;
459 if ( ldap_url_parse( url, &ludp ) != 0 ) {
460 ld->ld_errno = LDAP_PARAM_ERROR;
464 ber = ldap_build_search_req( ld, ludp->lud_dn, ludp->lud_scope,
465 ludp->lud_filter, ludp->lud_attrs, attrsonly, NULL, NULL,
474 if ( ludp->lud_host != NULL || ludp->lud_port != 0 ) {
475 if (( srv = (LDAPServer *)LDAP_CALLOC( 1, sizeof( LDAPServer )))
476 == NULL || ( srv->lsrv_host = LDAP_STRDUP( ludp->lud_host ==
477 NULL ? ld->ld_defhost : ludp->lud_host )) == NULL ) {
481 ld->ld_errno = LDAP_NO_MEMORY;
484 if ( ludp->lud_port == 0 ) {
485 srv->lsrv_port = ldap_int_global_options.ldo_defport;
487 srv->lsrv_port = ludp->lud_port;
495 err = ldap_send_server_request( ld, ber, ld->ld_msgid, NULL, srv,
499 ldap_free_urldesc( ludp );
506 ldap_url_search_st( LDAP *ld, LDAP_CONST char *url, int attrsonly,
507 struct timeval *timeout, LDAPMessage **res )
511 if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
512 return( ld->ld_errno );
515 if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 ) {
516 return( ld->ld_errno );
519 if ( ld->ld_errno == LDAP_TIMEOUT ) {
520 (void) ldap_abandon( ld, msgid );
521 ld->ld_errno = LDAP_TIMEOUT;
522 return( ld->ld_errno );
525 return( ldap_result2error( ld, *res, 0 ));
531 LDAP *ld, LDAP_CONST char *url, int attrsonly, LDAPMessage **res )
535 if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
536 return( ld->ld_errno );
539 if ( ldap_result( ld, msgid, 1, (struct timeval *)NULL, res ) == -1 ) {
540 return( ld->ld_errno );
543 return( ldap_result2error( ld, *res, 0 ));
548 hex_unescape( char *s )
551 * Remove URL hex escapes from s... done in place. The basic concept for
552 * this routine is borrowed from the WWW library HTUnEscape() routine.
556 for ( p = s; *s != '\0'; ++s ) {
558 if ( *++s != '\0' ) {
559 *p = unhex( *s ) << 4;
561 if ( *++s != '\0' ) {
576 return( c >= '0' && c <= '9' ? c - '0'
577 : c >= 'A' && c <= 'F' ? c - 'A' + 10