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((
43 unsigned long *properties,
48 ldap_is_ldap_url( LDAP_CONST char *url )
50 int enclosed, protocol;
51 unsigned long properties;
57 if( skip_url_prefix( url, &enclosed, &properties, &protocol) == NULL ) {
61 return !(properties & LDAP_URL_USE_SSL);
65 ldap_is_ldaps_url( LDAP_CONST char *url )
67 int enclosed, protocol;
68 unsigned long properties;
74 if( skip_url_prefix( url, &enclosed, &properties, &protocol) == NULL ) {
78 return (properties & LDAP_URL_USE_SSL);
85 unsigned long *properties,
90 * return non-zero if this looks like a LDAP URL; zero if not
91 * if non-zero returned, *urlp will be moved past "ldap://" part of URL
101 /* skip leading '<' (if any) */
109 /* skip leading "URL:" (if any) */
110 if ( strncasecmp( p, LDAP_URL_URLCOLON, LDAP_URL_URLCOLON_LEN ) == 0 )
112 p += LDAP_URL_URLCOLON_LEN;
117 /* check for "ldap://" prefix */
118 if ( strncasecmp( p, LDAP_URL_PREFIX, LDAP_URL_PREFIX_LEN ) == 0 ) {
119 /* skip over "ldap://" prefix and return success */
120 p += LDAP_URL_PREFIX_LEN;
121 *protocol = LDAP_PROTO_TCP;
125 /* check for "ldaps://" prefix */
126 if ( strncasecmp( p, LDAPS_URL_PREFIX, LDAPS_URL_PREFIX_LEN ) == 0 ) {
127 /* skip over "ldaps://" prefix and return success */
128 p += LDAPS_URL_PREFIX_LEN;
129 *protocol = LDAP_PROTO_TCP;
130 *properties |= LDAP_URL_USE_SSL;
134 /* check for "ldapi://" prefix */
135 if ( strncasecmp( p, LDAPI_URL_PREFIX, LDAPI_URL_PREFIX_LEN ) == 0 ) {
136 /* skip over "ldapi://" prefix and return success */
137 p += LDAPI_URL_PREFIX_LEN;
138 *protocol = LDAP_PROTO_LOCAL;
142 /* check for "ldapis://" prefix: should this be legal? */
143 if ( strncasecmp( p, LDAPIS_URL_PREFIX, LDAPIS_URL_PREFIX_LEN ) == 0 ) {
144 /* skip over "ldapis://" prefix and return success */
145 p += LDAPIS_URL_PREFIX_LEN;
146 *protocol = LDAP_PROTO_LOCAL;
147 *properties |= LDAP_URL_USE_SSL;
155 static int str2scope( const char *p )
157 if ( strcasecmp( p, "one" ) == 0 ) {
158 return LDAP_SCOPE_ONELEVEL;
160 } else if ( strcasecmp( p, "onetree" ) == 0 ) {
161 return LDAP_SCOPE_ONELEVEL;
163 } else if ( strcasecmp( p, "base" ) == 0 ) {
164 return LDAP_SCOPE_BASE;
166 } else if ( strcasecmp( p, "sub" ) == 0 ) {
167 return LDAP_SCOPE_SUBTREE;
169 } else if ( strcasecmp( p, "subtree" ) == 0 ) {
170 return LDAP_SCOPE_SUBTREE;
178 ldap_url_parse( LDAP_CONST char *url_in, LDAPURLDesc **ludpp )
181 * Pick apart the pieces of an LDAP URL.
186 int i, enclosed, protocol;
187 unsigned long properties;
191 if( url_in == NULL && ludpp == NULL ) {
192 return LDAP_URL_ERR_PARAM;
195 Debug( LDAP_DEBUG_TRACE, "ldap_url_parse(%s)\n", url_in, 0, 0 );
197 *ludpp = NULL; /* pessimistic */
199 url_tmp = skip_url_prefix( url_in, &enclosed, &properties, &protocol );
201 if ( url_tmp == NULL ) {
202 return LDAP_URL_ERR_NOTLDAP;
205 /* make working copy of the remainder of the URL */
206 if (( url = LDAP_STRDUP( url_tmp )) == NULL ) {
207 return( LDAP_URL_ERR_MEM );
211 p = &url[strlen(url)-1];
215 return LDAP_URL_ERR_BADENCLOSURE;
221 /* allocate return struct */
222 ludp = (LDAPURLDesc *)LDAP_CALLOC( 1, sizeof( LDAPURLDesc ));
224 if ( ludp == NULL ) {
226 return LDAP_URL_ERR_MEM;
229 ludp->lud_next = NULL;
230 ludp->lud_host = NULL;
233 ludp->lud_attrs = NULL;
234 ludp->lud_filter = NULL;
235 ludp->lud_properties = properties;
236 ludp->lud_protocol = protocol;
237 ludp->lud_scope = LDAP_SCOPE_BASE;
239 ludp->lud_filter = LDAP_STRDUP("(objectClass=*)");
241 if( ludp->lud_filter == NULL ) {
243 ldap_free_urldesc( ludp );
244 return LDAP_URL_ERR_MEM;
247 /* scan forward for '/' that marks end of hostport and begin. of dn */
248 p = strchr( url, '/' );
251 /* terminate hostport; point to start of dn */
255 if (( q = strchr( url, ':' )) != NULL ) {
257 ldap_pvt_hex_unescape( q );
261 ldap_free_urldesc( ludp );
262 return LDAP_URL_ERR_BADURL;
265 ludp->lud_port = atoi( q );
268 ldap_pvt_hex_unescape( url );
269 ludp->lud_host = LDAP_STRDUP( url );
271 if( ludp->lud_host == NULL ) {
273 ldap_free_urldesc( ludp );
274 return LDAP_URL_ERR_MEM;
280 return LDAP_URL_SUCCESS;
283 /* scan forward for '?' that may marks end of dn */
284 q = strchr( p, '?' );
287 /* terminate dn part */
293 ldap_pvt_hex_unescape( p );
294 ludp->lud_dn = LDAP_STRDUP( p );
296 ludp->lud_dn = LDAP_STRDUP( "" );
299 if( ludp->lud_dn == NULL ) {
301 ldap_free_urldesc( ludp );
302 return LDAP_URL_ERR_MEM;
309 return LDAP_URL_SUCCESS;
312 /* scan forward for '?' that may marks end of attributes */
314 q = strchr( p, '?' );
317 /* terminate attributes part */
322 /* parse attributes */
323 ldap_pvt_hex_unescape( p );
324 ludp->lud_attrs = ldap_str2charray( p, "," );
326 if( ludp->lud_attrs == NULL ) {
328 ldap_free_urldesc( ludp );
329 return LDAP_URL_ERR_BADATTRS;
337 return LDAP_URL_SUCCESS;
340 /* scan forward for '?' that may marks end of scope */
342 q = strchr( p, '?' );
345 /* terminate the scope part */
350 /* parse the scope */
351 ldap_pvt_hex_unescape( p );
352 ludp->lud_scope = str2scope( p );
354 if( ludp->lud_scope == -1 ) {
356 ldap_free_urldesc( ludp );
357 return LDAP_URL_ERR_BADSCOPE;
365 return LDAP_URL_SUCCESS;
368 /* scan forward for '?' that may marks end of filter */
370 q = strchr( p, '?' );
373 /* terminate the filter part */
378 /* parse the filter */
379 ldap_pvt_hex_unescape( p );
384 ldap_free_urldesc( ludp );
385 return LDAP_URL_ERR_BADFILTER;
388 LDAP_FREE( ludp->lud_filter );
389 ludp->lud_filter = LDAP_STRDUP( p );
391 if( ludp->lud_filter == NULL ) {
393 ldap_free_urldesc( ludp );
394 return LDAP_URL_ERR_MEM;
402 return LDAP_URL_SUCCESS;
405 /* scan forward for '?' that may marks end of extensions */
407 q = strchr( p, '?' );
412 ldap_free_urldesc( ludp );
413 return LDAP_URL_ERR_BADURL;
416 /* parse the extensions */
417 ludp->lud_exts = ldap_str2charray( p, "," );
419 if( ludp->lud_exts == NULL ) {
421 ldap_free_urldesc( ludp );
422 return LDAP_URL_ERR_BADEXTS;
425 for( i=0; ludp->lud_exts[i] != NULL; i++ ) {
426 ldap_pvt_hex_unescape( ludp->lud_exts[i] );
430 /* must have 1 or more */
431 ldap_charray_free( ludp->lud_exts );
433 ldap_free_urldesc( ludp );
434 return LDAP_URL_ERR_BADEXTS;
440 return LDAP_URL_SUCCESS;
444 ldap_url_dup ( LDAPURLDesc *ludp )
448 if ( ludp == NULL ) {
452 dest = LDAP_MALLOC( sizeof(LDAPURLDesc) );
457 dest->lud_next = NULL;
459 if ( ludp->lud_host != NULL ) {
460 dest->lud_host = LDAP_STRDUP( ludp->lud_host );
461 if (dest->lud_host == NULL) {
462 ldap_free_urldesc(dest);
467 if ( ludp->lud_dn != NULL ) {
468 dest->lud_dn = LDAP_STRDUP( ludp->lud_dn );
469 if (dest->lud_dn == NULL) {
470 ldap_free_urldesc(dest);
475 if ( ludp->lud_filter != NULL ) {
476 dest->lud_filter = LDAP_STRDUP( ludp->lud_filter );
477 if (dest->lud_filter == NULL) {
478 ldap_free_urldesc(dest);
483 if ( ludp->lud_attrs != NULL ) {
484 dest->lud_attrs = ldap_charray_dup( ludp->lud_attrs );
485 if (dest->lud_attrs == NULL) {
486 ldap_free_urldesc(dest);
491 if ( ludp->lud_exts != NULL ) {
492 dest->lud_exts = ldap_charray_dup( ludp->lud_exts );
493 if (dest->lud_exts == NULL) {
494 ldap_free_urldesc(dest);
503 ldap_url_duplist (LDAPURLDesc *ludlist)
505 LDAPURLDesc *dest, *tail, *ludp, *newludp;
509 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
510 newludp = ldap_url_dup(ludp);
511 if (newludp == NULL) {
512 ldap_free_urllist(dest);
518 tail->lud_next = newludp;
525 ldap_url_parselist (LDAPURLDesc **ludlist, const char *url )
534 return LDAP_PARAM_ERROR;
536 urls = ldap_str2charray((char *)url, ", ");
538 return LDAP_NO_MEMORY;
540 /* count the URLs... */
541 for (i = 0; urls[i] != NULL; i++) ;
542 /* ...and put them in the "stack" backward */
544 rc = ldap_url_parse( urls[i], &ludp );
546 ldap_charray_free(urls);
547 ldap_free_urllist(*ludlist);
551 ludp->lud_next = *ludlist;
554 ldap_charray_free(urls);
559 ldap_url_parsehosts (LDAPURLDesc **ludlist, const char *hosts )
568 return LDAP_PARAM_ERROR;
570 specs = ldap_str2charray((char *)hosts, ", ");
572 return LDAP_NO_MEMORY;
574 /* count the URLs... */
575 for (i = 0; specs[i] != NULL; i++) ;
576 /* ...and put them in the "stack" backward */
578 ludp = LDAP_CALLOC( 1, sizeof(LDAPURLDesc) );
580 ldap_charray_free(specs);
581 ldap_free_urllist(*ludlist);
583 return LDAP_NO_MEMORY;
585 ludp->lud_host = specs[i];
587 p = strchr(ludp->lud_host, ':');
590 ldap_pvt_hex_unescape(p);
591 ludp->lud_port = atoi(p);
593 ldap_pvt_hex_unescape(ludp->lud_host);
594 ludp->lud_protocol = LDAP_PROTO_TCP;
595 ludp->lud_properties = 0;
596 ludp->lud_next = *ludlist;
600 /* this should be an array of NULLs now */
601 ldap_charray_free(specs);
606 ldap_url_list2hosts (LDAPURLDesc *ludlist)
610 char *s, *p, buf[32]; /* big enough to hold a long decimal # (overkill) */
615 /* figure out how big the string is */
616 size = 1; /* nul-term */
617 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
618 size += strlen(ludp->lud_host) + 1; /* host and space */
619 if (ludp->lud_port != 0)
620 size += sprintf(buf, ":%d", ludp->lud_port);
622 s = LDAP_MALLOC(size);
627 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
628 strcpy(p, ludp->lud_host);
629 p += strlen(ludp->lud_host);
630 if (ludp->lud_port != 0)
631 p += sprintf(p, ":%d", ludp->lud_port);
635 p--; /* nuke that extra space */
641 ldap_url_list2urls (LDAPURLDesc *ludlist)
645 char *s, *p, buf[32]; /* big enough to hold a long decimal # (overkill) */
650 /* figure out how big the string is */
651 size = 1; /* nul-term */
652 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
653 size += strlen(ludp->lud_host) + 1 + sizeof("ldapis:///"); /* prefix, host, /, and space */
654 if (ludp->lud_port != 0)
655 size += sprintf(buf, ":%d", ludp->lud_port);
657 s = LDAP_MALLOC(size);
662 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
663 p += sprintf(p, "ldap%s://%s", (ludp->lud_properties & LDAP_URL_USE_SSL) ? "s" : "", ludp->lud_host);
664 if (ludp->lud_port != 0)
665 p += sprintf(p, ":%d", ludp->lud_port);
670 p--; /* nuke that extra space */
676 ldap_free_urllist( LDAPURLDesc *ludlist )
678 LDAPURLDesc *ludp, *next;
680 for (ludp = ludlist; ludp != NULL; ludp = next) {
681 next = ludp->lud_next;
682 ldap_free_urldesc(ludp);
687 ldap_free_urldesc( LDAPURLDesc *ludp )
689 if ( ludp == NULL ) {
693 if ( ludp->lud_host != NULL ) {
694 LDAP_FREE( ludp->lud_host );
697 if ( ludp->lud_dn != NULL ) {
698 LDAP_FREE( ludp->lud_dn );
701 if ( ludp->lud_filter != NULL ) {
702 LDAP_FREE( ludp->lud_filter);
705 if ( ludp->lud_attrs != NULL ) {
706 LDAP_VFREE( ludp->lud_attrs );
709 if ( ludp->lud_exts != NULL ) {
710 LDAP_VFREE( ludp->lud_exts );
719 ldap_url_search( LDAP *ld, LDAP_CONST char *url, int attrsonly )
725 if ( ldap_url_parse( url, &ludp ) != 0 ) {
726 ld->ld_errno = LDAP_PARAM_ERROR;
730 ber = ldap_build_search_req( ld, ludp->lud_dn, ludp->lud_scope,
731 ludp->lud_filter, ludp->lud_attrs, attrsonly, NULL, NULL,
737 err = ldap_send_server_request(
738 ld, ber, ld->ld_msgid, NULL,
739 (ludp->lud_host != NULL || ludp->lud_port != 0)
744 ldap_free_urldesc( ludp );
750 ldap_url_search_st( LDAP *ld, LDAP_CONST char *url, int attrsonly,
751 struct timeval *timeout, LDAPMessage **res )
755 if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
756 return( ld->ld_errno );
759 if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 ) {
760 return( ld->ld_errno );
763 if ( ld->ld_errno == LDAP_TIMEOUT ) {
764 (void) ldap_abandon( ld, msgid );
765 ld->ld_errno = LDAP_TIMEOUT;
766 return( ld->ld_errno );
769 return( ldap_result2error( ld, *res, 0 ));
775 LDAP *ld, LDAP_CONST char *url, int attrsonly, LDAPMessage **res )
779 if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
780 return( ld->ld_errno );
783 if ( ldap_result( ld, msgid, 1, (struct timeval *)NULL, res ) == -1 ) {
784 return( ld->ld_errno );
787 return( ldap_result2error( ld, *res, 0 ));
792 ldap_pvt_hex_unescape( char *s )
795 * Remove URL hex escapes from s... done in place. The basic concept for
796 * this routine is borrowed from the WWW library HTUnEscape() routine.
800 for ( p = s; *s != '\0'; ++s ) {
802 if ( *++s != '\0' ) {
803 *p = ldap_pvt_unhex( *s ) << 4;
805 if ( *++s != '\0' ) {
806 *p++ += ldap_pvt_unhex( *s );
818 ldap_pvt_unhex( int c )
820 return( c >= '0' && c <= '9' ? c - '0'
821 : c >= 'A' && c <= 'F' ? c - 'A' + 10