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_next = NULL;
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 ) {
232 ldap_pvt_hex_unescape( q );
236 ldap_free_urldesc( ludp );
237 return LDAP_URL_ERR_BADURL;
240 ludp->lud_port = atoi( q );
243 ldap_pvt_hex_unescape( url );
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 */
268 ldap_pvt_hex_unescape( p );
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 */
298 ldap_pvt_hex_unescape( p );
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 */
326 ldap_pvt_hex_unescape( p );
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 */
354 ldap_pvt_hex_unescape( p );
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 ldap_pvt_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;
419 ldap_url_dup ( LDAPURLDesc *ludp )
423 if ( ludp == NULL ) {
427 dest = LDAP_CALLOC( 1, sizeof(LDAPURLDesc) );
431 if ( ludp->lud_host != NULL ) {
432 dest->lud_host = LDAP_STRDUP( ludp->lud_host );
433 if (dest->lud_host == NULL) {
434 ldap_free_urldesc(dest);
439 if ( ludp->lud_dn != NULL ) {
440 dest->lud_dn = LDAP_STRDUP( ludp->lud_dn );
441 if (dest->lud_dn == NULL) {
442 ldap_free_urldesc(dest);
447 if ( ludp->lud_filter != NULL ) {
448 dest->lud_filter = LDAP_STRDUP( ludp->lud_filter );
449 if (dest->lud_filter == NULL) {
450 ldap_free_urldesc(dest);
455 if ( ludp->lud_attrs != NULL ) {
456 dest->lud_attrs = ldap_charray_dup( ludp->lud_attrs );
457 if (dest->lud_attrs == NULL) {
458 ldap_free_urldesc(dest);
463 if ( ludp->lud_exts != NULL ) {
464 dest->lud_exts = ldap_charray_dup( ludp->lud_exts );
465 if (dest->lud_exts == NULL) {
466 ldap_free_urldesc(dest);
471 dest->lud_ldaps = ludp->lud_ldaps;
472 dest->lud_port = ludp->lud_port;
473 dest->lud_scope = ludp->lud_scope;
479 ldap_url_duplist (LDAPURLDesc *ludlist)
481 LDAPURLDesc *dest, *tail, *ludp, *newludp;
485 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
486 newludp = ldap_url_dup(ludp);
487 if (newludp == NULL) {
488 ldap_free_urllist(dest);
494 tail->lud_next = newludp;
501 ldap_url_parselist (LDAPURLDesc **ludlist, const char *url )
510 return LDAP_PARAM_ERROR;
512 urls = ldap_str2charray((char *)url, ", ");
514 return LDAP_NO_MEMORY;
516 /* count the URLs... */
517 for (i = 0; urls[i] != NULL; i++) ;
518 /* ...and put them in the "stack" backward */
520 rc = ldap_url_parse( urls[i], &ludp );
522 ldap_charray_free(urls);
523 ldap_free_urllist(*ludlist);
527 ludp->lud_next = *ludlist;
530 ldap_charray_free(urls);
535 ldap_url_parsehosts (LDAPURLDesc **ludlist, const char *hosts )
544 return LDAP_PARAM_ERROR;
546 specs = ldap_str2charray((char *)hosts, ", ");
548 return LDAP_NO_MEMORY;
550 /* count the URLs... */
551 for (i = 0; specs[i] != NULL; i++) ;
552 /* ...and put them in the "stack" backward */
554 ludp = LDAP_CALLOC( 1, sizeof(LDAPURLDesc) );
556 ldap_charray_free(specs);
557 ldap_free_urllist(*ludlist);
559 return LDAP_NO_MEMORY;
561 ludp->lud_host = specs[i];
563 p = strchr(ludp->lud_host, ':');
566 ludp->lud_port = atoi(p);
568 ludp->lud_ldaps = -1; /* unknown (use TLS default) */
569 ludp->lud_next = *ludlist;
573 /* this should be an array of NULLs now */
574 ldap_charray_free(specs);
579 ldap_url_list2hosts (LDAPURLDesc *ludlist)
583 char *s, *p, buf[32]; /* big enough to hold a long decimal # (overkill) */
588 /* figure out how big the string is */
589 size = 1; /* nul-term */
590 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
591 size += strlen(ludp->lud_host) + 1; /* host and space */
592 if (ludp->lud_port != 0)
593 size += sprintf(buf, ":%d", ludp->lud_port);
595 s = LDAP_MALLOC(size);
600 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
601 strcpy(p, ludp->lud_host);
602 p += strlen(ludp->lud_host);
603 if (ludp->lud_port != 0)
604 p += sprintf(p, ":%d", ludp->lud_port);
608 p--; /* nuke that extra space */
614 ldap_url_list2urls (LDAPURLDesc *ludlist)
618 char *s, *p, buf[32]; /* big enough to hold a long decimal # (overkill) */
623 /* figure out how big the string is */
624 size = 1; /* nul-term */
625 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
626 size += strlen(ludp->lud_host) + 1 + sizeof("ldapis:///"); /* prefix, host, /, and space */
627 if (ludp->lud_port != 0)
628 size += sprintf(buf, ":%d", ludp->lud_port);
630 s = LDAP_MALLOC(size);
635 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
636 p += sprintf(p, "ldap%s://%s", (ludp->lud_ldaps == 1) ? "s" : "", ludp->lud_host);
637 if (ludp->lud_port != 0)
638 p += sprintf(p, ":%d", ludp->lud_port);
643 p--; /* nuke that extra space */
649 ldap_free_urllist( LDAPURLDesc *ludlist )
651 LDAPURLDesc *ludp, *next;
653 for (ludp = ludlist; ludp != NULL; ludp = next) {
654 next = ludp->lud_next;
655 ldap_free_urldesc(ludp);
660 ldap_free_urldesc( LDAPURLDesc *ludp )
662 if ( ludp == NULL ) {
666 if ( ludp->lud_host != NULL ) {
667 LDAP_FREE( ludp->lud_host );
670 if ( ludp->lud_dn != NULL ) {
671 LDAP_FREE( ludp->lud_dn );
674 if ( ludp->lud_filter != NULL ) {
675 LDAP_FREE( ludp->lud_filter);
678 if ( ludp->lud_attrs != NULL ) {
679 LDAP_VFREE( ludp->lud_attrs );
682 if ( ludp->lud_exts != NULL ) {
683 LDAP_VFREE( ludp->lud_exts );
692 ldap_url_search( LDAP *ld, LDAP_CONST char *url, int attrsonly )
698 if ( ldap_url_parse( url, &ludp ) != 0 ) {
699 ld->ld_errno = LDAP_PARAM_ERROR;
703 ber = ldap_build_search_req( ld, ludp->lud_dn, ludp->lud_scope,
704 ludp->lud_filter, ludp->lud_attrs, attrsonly, NULL, NULL,
710 err = ldap_send_server_request(
711 ld, ber, ld->ld_msgid, NULL,
712 (ludp->lud_host != NULL || ludp->lud_port != 0)
717 ldap_free_urldesc( ludp );
723 ldap_url_search_st( LDAP *ld, LDAP_CONST char *url, int attrsonly,
724 struct timeval *timeout, LDAPMessage **res )
728 if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
729 return( ld->ld_errno );
732 if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 ) {
733 return( ld->ld_errno );
736 if ( ld->ld_errno == LDAP_TIMEOUT ) {
737 (void) ldap_abandon( ld, msgid );
738 ld->ld_errno = LDAP_TIMEOUT;
739 return( ld->ld_errno );
742 return( ldap_result2error( ld, *res, 0 ));
748 LDAP *ld, LDAP_CONST char *url, int attrsonly, LDAPMessage **res )
752 if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
753 return( ld->ld_errno );
756 if ( ldap_result( ld, msgid, 1, (struct timeval *)NULL, res ) == -1 ) {
757 return( ld->ld_errno );
760 return( ldap_result2error( ld, *res, 0 ));
765 ldap_pvt_hex_unescape( char *s )
768 * Remove URL hex escapes from s... done in place. The basic concept for
769 * this routine is borrowed from the WWW library HTUnEscape() routine.
773 for ( p = s; *s != '\0'; ++s ) {
775 if ( *++s != '\0' ) {
776 *p = ldap_pvt_unhex( *s ) << 4;
778 if ( *++s != '\0' ) {
779 *p++ += ldap_pvt_unhex( *s );
791 ldap_pvt_unhex( int c )
793 return( c >= '0' && c <= '9' ? c - '0'
794 : c >= 'A' && c <= 'F' ? c - 'A' + 10