3 * Copyright 1998-2000 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[is]://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 const char **scheme ));
45 int ldap_pvt_url_scheme2proto( const char *scheme )
49 if( scheme == NULL ) {
53 if( strcmp("ldap", scheme) == 0 ) {
54 return LDAP_PROTO_TCP;
57 if( strcmp("ldapi", scheme) == 0 ) {
58 return LDAP_PROTO_IPC;
61 if( strcmp("ldaps", scheme) == 0 ) {
62 return LDAP_PROTO_TCP;
68 int ldap_pvt_url_scheme2tls( const char *scheme )
72 if( scheme == NULL ) {
76 return strcmp("ldaps", scheme) == 0;
80 ldap_is_ldap_url( LDAP_CONST char *url )
89 if( skip_url_prefix( url, &enclosed, &scheme ) == NULL ) {
97 ldap_is_ldaps_url( LDAP_CONST char *url )
106 if( skip_url_prefix( url, &enclosed, &scheme ) == NULL ) {
110 return strcmp(scheme, "ldaps") == 0;
114 ldap_is_ldapi_url( LDAP_CONST char *url )
123 if( skip_url_prefix( url, &enclosed, &scheme ) == NULL ) {
127 return strcmp(scheme, "ldapi") == 0;
134 const char **scheme )
137 * return non-zero if this looks like a LDAP URL; zero if not
138 * if non-zero returned, *urlp will be moved past "ldap://" part of URL
148 /* skip leading '<' (if any) */
156 /* skip leading "URL:" (if any) */
157 if ( strncasecmp( p, LDAP_URL_URLCOLON, LDAP_URL_URLCOLON_LEN ) == 0 )
159 p += LDAP_URL_URLCOLON_LEN;
162 /* check for "ldap://" prefix */
163 if ( strncasecmp( p, LDAP_URL_PREFIX, LDAP_URL_PREFIX_LEN ) == 0 ) {
164 /* skip over "ldap://" prefix and return success */
165 p += LDAP_URL_PREFIX_LEN;
170 /* check for "ldaps://" prefix */
171 if ( strncasecmp( p, LDAPS_URL_PREFIX, LDAPS_URL_PREFIX_LEN ) == 0 ) {
172 /* skip over "ldaps://" prefix and return success */
173 p += LDAPS_URL_PREFIX_LEN;
178 /* check for "ldapi://" prefix */
179 if ( strncasecmp( p, LDAPI_URL_PREFIX, LDAPI_URL_PREFIX_LEN ) == 0 ) {
180 /* skip over "ldapi://" prefix and return success */
181 p += LDAPI_URL_PREFIX_LEN;
190 static int str2scope( const char *p )
192 if ( strcasecmp( p, "one" ) == 0 ) {
193 return LDAP_SCOPE_ONELEVEL;
195 } else if ( strcasecmp( p, "onetree" ) == 0 ) {
196 return LDAP_SCOPE_ONELEVEL;
198 } else if ( strcasecmp( p, "base" ) == 0 ) {
199 return LDAP_SCOPE_BASE;
201 } else if ( strcasecmp( p, "sub" ) == 0 ) {
202 return LDAP_SCOPE_SUBTREE;
204 } else if ( strcasecmp( p, "subtree" ) == 0 ) {
205 return LDAP_SCOPE_SUBTREE;
213 ldap_url_parse( LDAP_CONST char *url_in, LDAPURLDesc **ludpp )
216 * Pick apart the pieces of an LDAP URL.
222 const char *scheme = NULL;
226 if( url_in == NULL && ludpp == NULL ) {
227 return LDAP_URL_ERR_PARAM;
230 #ifndef LDAP_INT_IN_KERNEL
231 /* Global options may not be created yet
232 * We can't test if the global options are initialized
233 * because a call to LDAP_INT_GLOBAL_OPT() will try to allocate
234 * the options and cause infinite recursion
236 Debug( LDAP_DEBUG_TRACE, "ldap_url_parse(%s)\n", url_in, 0, 0 );
239 *ludpp = NULL; /* pessimistic */
241 url_tmp = skip_url_prefix( url_in, &enclosed, &scheme );
243 if ( url_tmp == NULL ) {
244 return LDAP_URL_ERR_BADSCHEME;
249 /* make working copy of the remainder of the URL */
250 if (( url = LDAP_STRDUP( url_tmp )) == NULL ) {
251 return( LDAP_URL_ERR_MEM );
255 p = &url[strlen(url)-1];
259 return LDAP_URL_ERR_BADENCLOSURE;
265 /* allocate return struct */
266 ludp = (LDAPURLDesc *)LDAP_CALLOC( 1, sizeof( LDAPURLDesc ));
268 if ( ludp == NULL ) {
270 return LDAP_URL_ERR_MEM;
273 ludp->lud_next = NULL;
274 ludp->lud_host = NULL;
277 ludp->lud_attrs = NULL;
278 ludp->lud_filter = NULL;
279 ludp->lud_scope = LDAP_SCOPE_BASE;
280 ludp->lud_filter = NULL;
282 ludp->lud_scheme = LDAP_STRDUP( scheme );
284 if ( ludp->lud_scheme == NULL ) {
286 ldap_free_urldesc( ludp );
287 return LDAP_URL_ERR_MEM;
290 /* scan forward for '/' that marks end of hostport and begin. of dn */
291 p = strchr( url, '/' );
294 /* terminate hostport; point to start of dn */
298 if (( q = strchr( url, ':' )) != NULL ) {
300 ldap_pvt_hex_unescape( q );
304 ldap_free_urldesc( ludp );
305 return LDAP_URL_ERR_BADURL;
308 ludp->lud_port = atoi( q );
311 ldap_pvt_hex_unescape( url );
312 ludp->lud_host = LDAP_STRDUP( url );
314 if( ludp->lud_host == NULL ) {
316 ldap_free_urldesc( ludp );
317 return LDAP_URL_ERR_MEM;
321 * Kludge. ldap://111.222.333.444:389??cn=abc,o=company
323 * On early Novell releases, search references/referrals were returned
324 * in this format, i.e., the dn was kind of in the scope position,
325 * but the required slash is missing. The whole thing is illegal syntax,
326 * but we need to account for it. Fortunately it can't be confused with
329 if( (p == NULL) && ((q = strchr( q, '?')) != NULL)) {
331 /* ? immediately followed by question */
336 ldap_pvt_hex_unescape( q );
337 ludp->lud_dn = LDAP_STRDUP( q );
339 ludp->lud_dn = LDAP_STRDUP( "" );
342 if( ludp->lud_dn == NULL ) {
344 ldap_free_urldesc( ludp );
345 return LDAP_URL_ERR_MEM;
353 return LDAP_URL_SUCCESS;
356 /* scan forward for '?' that may marks end of dn */
357 q = strchr( p, '?' );
360 /* terminate dn part */
366 ldap_pvt_hex_unescape( p );
367 ludp->lud_dn = LDAP_STRDUP( p );
369 ludp->lud_dn = LDAP_STRDUP( "" );
372 if( ludp->lud_dn == NULL ) {
374 ldap_free_urldesc( ludp );
375 return LDAP_URL_ERR_MEM;
382 return LDAP_URL_SUCCESS;
385 /* scan forward for '?' that may marks end of attributes */
387 q = strchr( p, '?' );
390 /* terminate attributes part */
395 /* parse attributes */
396 ldap_pvt_hex_unescape( p );
397 ludp->lud_attrs = ldap_str2charray( p, "," );
399 if( ludp->lud_attrs == NULL ) {
401 ldap_free_urldesc( ludp );
402 return LDAP_URL_ERR_BADATTRS;
410 return LDAP_URL_SUCCESS;
413 /* scan forward for '?' that may marks end of scope */
415 q = strchr( p, '?' );
418 /* terminate the scope part */
423 /* parse the scope */
424 ldap_pvt_hex_unescape( p );
425 ludp->lud_scope = str2scope( p );
427 if( ludp->lud_scope == -1 ) {
429 ldap_free_urldesc( ludp );
430 return LDAP_URL_ERR_BADSCOPE;
438 return LDAP_URL_SUCCESS;
441 /* scan forward for '?' that may marks end of filter */
443 q = strchr( p, '?' );
446 /* terminate the filter part */
451 /* parse the filter */
452 ldap_pvt_hex_unescape( p );
457 ldap_free_urldesc( ludp );
458 return LDAP_URL_ERR_BADFILTER;
461 LDAP_FREE( ludp->lud_filter );
462 ludp->lud_filter = LDAP_STRDUP( p );
464 if( ludp->lud_filter == NULL ) {
466 ldap_free_urldesc( ludp );
467 return LDAP_URL_ERR_MEM;
475 return LDAP_URL_SUCCESS;
478 /* scan forward for '?' that may marks end of extensions */
480 q = strchr( p, '?' );
485 ldap_free_urldesc( ludp );
486 return LDAP_URL_ERR_BADURL;
489 /* parse the extensions */
490 ludp->lud_exts = ldap_str2charray( p, "," );
492 if( ludp->lud_exts == NULL ) {
494 ldap_free_urldesc( ludp );
495 return LDAP_URL_ERR_BADEXTS;
498 for( i=0; ludp->lud_exts[i] != NULL; i++ ) {
499 ldap_pvt_hex_unescape( ludp->lud_exts[i] );
503 /* must have 1 or more */
504 ldap_charray_free( ludp->lud_exts );
506 ldap_free_urldesc( ludp );
507 return LDAP_URL_ERR_BADEXTS;
513 return LDAP_URL_SUCCESS;
517 ldap_url_dup ( LDAPURLDesc *ludp )
521 if ( ludp == NULL ) {
525 dest = LDAP_MALLOC( sizeof(LDAPURLDesc) );
530 dest->lud_scheme = NULL;
531 dest->lud_host = NULL;
533 dest->lud_filter = NULL;
534 dest->lud_attrs = NULL;
535 dest->lud_exts = NULL;
536 dest->lud_next = NULL;
538 if ( ludp->lud_scheme != NULL ) {
539 dest->lud_scheme = LDAP_STRDUP( ludp->lud_scheme );
540 if (dest->lud_scheme == NULL) {
541 ldap_free_urldesc(dest);
546 if ( ludp->lud_host != NULL ) {
547 dest->lud_host = LDAP_STRDUP( ludp->lud_host );
548 if (dest->lud_host == NULL) {
549 ldap_free_urldesc(dest);
554 if ( ludp->lud_dn != NULL ) {
555 dest->lud_dn = LDAP_STRDUP( ludp->lud_dn );
556 if (dest->lud_dn == NULL) {
557 ldap_free_urldesc(dest);
562 if ( ludp->lud_filter != NULL ) {
563 dest->lud_filter = LDAP_STRDUP( ludp->lud_filter );
564 if (dest->lud_filter == NULL) {
565 ldap_free_urldesc(dest);
570 if ( ludp->lud_attrs != NULL ) {
571 dest->lud_attrs = ldap_charray_dup( ludp->lud_attrs );
572 if (dest->lud_attrs == NULL) {
573 ldap_free_urldesc(dest);
578 if ( ludp->lud_exts != NULL ) {
579 dest->lud_exts = ldap_charray_dup( ludp->lud_exts );
580 if (dest->lud_exts == NULL) {
581 ldap_free_urldesc(dest);
590 ldap_url_duplist (LDAPURLDesc *ludlist)
592 LDAPURLDesc *dest, *tail, *ludp, *newludp;
596 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
597 newludp = ldap_url_dup(ludp);
598 if (newludp == NULL) {
599 ldap_free_urllist(dest);
605 tail->lud_next = newludp;
612 ldap_url_parselist (LDAPURLDesc **ludlist, const char *url )
621 return LDAP_PARAM_ERROR;
623 urls = ldap_str2charray((char *)url, ", ");
625 return LDAP_NO_MEMORY;
627 /* count the URLs... */
628 for (i = 0; urls[i] != NULL; i++) ;
629 /* ...and put them in the "stack" backward */
631 rc = ldap_url_parse( urls[i], &ludp );
633 ldap_charray_free(urls);
634 ldap_free_urllist(*ludlist);
638 ludp->lud_next = *ludlist;
641 ldap_charray_free(urls);
646 ldap_url_parsehosts (LDAPURLDesc **ludlist, const char *hosts )
655 return LDAP_PARAM_ERROR;
657 specs = ldap_str2charray((char *)hosts, ", ");
659 return LDAP_NO_MEMORY;
661 /* count the URLs... */
662 for (i = 0; specs[i] != NULL; i++) /* EMPTY */;
664 /* ...and put them in the "stack" backward */
666 ludp = LDAP_CALLOC( 1, sizeof(LDAPURLDesc) );
668 ldap_charray_free(specs);
669 ldap_free_urllist(*ludlist);
671 return LDAP_NO_MEMORY;
673 ludp->lud_host = specs[i];
675 p = strchr(ludp->lud_host, ':');
678 ldap_pvt_hex_unescape(p);
679 ludp->lud_port = atoi(p);
681 ldap_pvt_hex_unescape(ludp->lud_host);
682 ludp->lud_scheme = LDAP_STRDUP("ldap");
683 ludp->lud_next = *ludlist;
687 /* this should be an array of NULLs now */
688 ldap_charray_free(specs);
693 ldap_url_list2hosts (LDAPURLDesc *ludlist)
697 char *s, *p, buf[32]; /* big enough to hold a long decimal # (overkill) */
702 /* figure out how big the string is */
703 size = 1; /* nul-term */
704 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
705 size += strlen(ludp->lud_host) + 1; /* host and space */
706 if (ludp->lud_port != 0)
707 size += sprintf(buf, ":%d", ludp->lud_port);
709 s = LDAP_MALLOC(size);
714 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
715 strcpy(p, ludp->lud_host);
716 p += strlen(ludp->lud_host);
717 if (ludp->lud_port != 0)
718 p += sprintf(p, ":%d", ludp->lud_port);
722 p--; /* nuke that extra space */
729 LDAPURLDesc *ludlist )
733 char *s, *p, buf[32]; /* big enough to hold a long decimal # (overkill) */
738 /* figure out how big the string is */
739 size = 1; /* nul-term */
740 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
741 size += strlen(ludp->lud_scheme) + strlen(ludp->lud_host);
742 size += sizeof(":/// ");
744 if (ludp->lud_port != 0) {
745 size += sprintf(buf, ":%d", ludp->lud_port);
749 s = LDAP_MALLOC(size);
755 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
756 p += sprintf(p, "%s://%s", ludp->lud_scheme, ludp->lud_host);
757 if (ludp->lud_port != 0)
758 p += sprintf(p, ":%d", ludp->lud_port);
763 p--; /* nuke that extra space */
769 ldap_free_urllist( LDAPURLDesc *ludlist )
771 LDAPURLDesc *ludp, *next;
773 for (ludp = ludlist; ludp != NULL; ludp = next) {
774 next = ludp->lud_next;
775 ldap_free_urldesc(ludp);
780 ldap_free_urldesc( LDAPURLDesc *ludp )
782 if ( ludp == NULL ) {
786 if ( ludp->lud_scheme != NULL ) {
787 LDAP_FREE( ludp->lud_scheme );
790 if ( ludp->lud_host != NULL ) {
791 LDAP_FREE( ludp->lud_host );
794 if ( ludp->lud_dn != NULL ) {
795 LDAP_FREE( ludp->lud_dn );
798 if ( ludp->lud_filter != NULL ) {
799 LDAP_FREE( ludp->lud_filter);
802 if ( ludp->lud_attrs != NULL ) {
803 LDAP_VFREE( ludp->lud_attrs );
806 if ( ludp->lud_exts != NULL ) {
807 LDAP_VFREE( ludp->lud_exts );
816 ldap_url_search( LDAP *ld, LDAP_CONST char *url, int attrsonly )
823 if ( ldap_url_parse( url, &ludp ) != 0 ) {
824 ld->ld_errno = LDAP_PARAM_ERROR;
828 ber = ldap_build_search_req( ld, ludp->lud_dn, ludp->lud_scope,
829 ludp->lud_filter, ludp->lud_attrs, attrsonly, NULL, NULL,
835 bind.ri_request = LDAP_REQ_SEARCH;
836 bind.ri_msgid = ld->ld_msgid;
837 bind.ri_url = (char *)url;
838 err = ldap_send_server_request(
839 ld, ber, ld->ld_msgid, NULL,
840 (ludp->lud_host != NULL || ludp->lud_port != 0)
845 ldap_free_urldesc( ludp );
851 ldap_url_search_st( LDAP *ld, LDAP_CONST char *url, int attrsonly,
852 struct timeval *timeout, LDAPMessage **res )
856 if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
857 return( ld->ld_errno );
860 if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 ) {
861 return( ld->ld_errno );
864 if ( ld->ld_errno == LDAP_TIMEOUT ) {
865 (void) ldap_abandon( ld, msgid );
866 ld->ld_errno = LDAP_TIMEOUT;
867 return( ld->ld_errno );
870 return( ldap_result2error( ld, *res, 0 ));
876 LDAP *ld, LDAP_CONST char *url, int attrsonly, LDAPMessage **res )
880 if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
881 return( ld->ld_errno );
884 if ( ldap_result( ld, msgid, 1, (struct timeval *)NULL, res ) == -1 ) {
885 return( ld->ld_errno );
888 return( ldap_result2error( ld, *res, 0 ));
893 ldap_pvt_hex_unescape( char *s )
896 * Remove URL hex escapes from s... done in place. The basic concept for
897 * this routine is borrowed from the WWW library HTUnEscape() routine.
901 for ( p = s; *s != '\0'; ++s ) {
903 if ( *++s != '\0' ) {
904 *p = ldap_pvt_unhex( *s ) << 4;
906 if ( *++s != '\0' ) {
907 *p++ += ldap_pvt_unhex( *s );
919 ldap_pvt_unhex( int c )
921 return( c >= '0' && c <= '9' ? c - '0'
922 : c >= 'A' && c <= 'F' ? c - 'A' + 10