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 Debug( LDAP_DEBUG_TRACE, "ldap_url_parse(%s)\n", url_in, 0, 0 );
232 *ludpp = NULL; /* pessimistic */
234 url_tmp = skip_url_prefix( url_in, &enclosed, &scheme );
236 if ( url_tmp == NULL ) {
237 return LDAP_URL_ERR_BADSCHEME;
242 /* make working copy of the remainder of the URL */
243 if (( url = LDAP_STRDUP( url_tmp )) == NULL ) {
244 return( LDAP_URL_ERR_MEM );
248 p = &url[strlen(url)-1];
252 return LDAP_URL_ERR_BADENCLOSURE;
258 /* allocate return struct */
259 ludp = (LDAPURLDesc *)LDAP_CALLOC( 1, sizeof( LDAPURLDesc ));
261 if ( ludp == NULL ) {
263 return LDAP_URL_ERR_MEM;
266 ludp->lud_next = NULL;
267 ludp->lud_host = NULL;
270 ludp->lud_attrs = NULL;
271 ludp->lud_filter = NULL;
272 ludp->lud_scope = LDAP_SCOPE_BASE;
273 ludp->lud_filter = NULL;
275 ludp->lud_scheme = LDAP_STRDUP( scheme );
277 if ( ludp->lud_scheme == NULL ) {
279 ldap_free_urldesc( ludp );
280 return LDAP_URL_ERR_MEM;
283 /* scan forward for '/' that marks end of hostport and begin. of dn */
284 p = strchr( url, '/' );
287 /* terminate hostport; point to start of dn */
291 if (( q = strchr( url, ':' )) != NULL ) {
293 ldap_pvt_hex_unescape( q );
297 ldap_free_urldesc( ludp );
298 return LDAP_URL_ERR_BADURL;
301 ludp->lud_port = atoi( q );
304 ldap_pvt_hex_unescape( url );
305 ludp->lud_host = LDAP_STRDUP( url );
307 if( ludp->lud_host == NULL ) {
309 ldap_free_urldesc( ludp );
310 return LDAP_URL_ERR_MEM;
314 * Kludge. ldap://111.222.333.444:389??cn=abc,o=company
316 * On early Novell releases, search references/referrals were returned
317 * in this format, i.e., the dn was kind of in the scope position,
318 * but the required slash is missing. The whole thing is illegal syntax,
319 * but we need to account for it. Fortunately it can't be confused with
322 if( (p == NULL) && ((q = strchr( q, '?')) != NULL)) {
324 /* ? immediately followed by question */
329 ldap_pvt_hex_unescape( q );
330 ludp->lud_dn = LDAP_STRDUP( q );
332 ludp->lud_dn = LDAP_STRDUP( "" );
335 if( ludp->lud_dn == NULL ) {
337 ldap_free_urldesc( ludp );
338 return LDAP_URL_ERR_MEM;
346 return LDAP_URL_SUCCESS;
349 /* scan forward for '?' that may marks end of dn */
350 q = strchr( p, '?' );
353 /* terminate dn part */
359 ldap_pvt_hex_unescape( p );
360 ludp->lud_dn = LDAP_STRDUP( p );
362 ludp->lud_dn = LDAP_STRDUP( "" );
365 if( ludp->lud_dn == NULL ) {
367 ldap_free_urldesc( ludp );
368 return LDAP_URL_ERR_MEM;
375 return LDAP_URL_SUCCESS;
378 /* scan forward for '?' that may marks end of attributes */
380 q = strchr( p, '?' );
383 /* terminate attributes part */
388 /* parse attributes */
389 ldap_pvt_hex_unescape( p );
390 ludp->lud_attrs = ldap_str2charray( p, "," );
392 if( ludp->lud_attrs == NULL ) {
394 ldap_free_urldesc( ludp );
395 return LDAP_URL_ERR_BADATTRS;
403 return LDAP_URL_SUCCESS;
406 /* scan forward for '?' that may marks end of scope */
408 q = strchr( p, '?' );
411 /* terminate the scope part */
416 /* parse the scope */
417 ldap_pvt_hex_unescape( p );
418 ludp->lud_scope = str2scope( p );
420 if( ludp->lud_scope == -1 ) {
422 ldap_free_urldesc( ludp );
423 return LDAP_URL_ERR_BADSCOPE;
431 return LDAP_URL_SUCCESS;
434 /* scan forward for '?' that may marks end of filter */
436 q = strchr( p, '?' );
439 /* terminate the filter part */
444 /* parse the filter */
445 ldap_pvt_hex_unescape( p );
450 ldap_free_urldesc( ludp );
451 return LDAP_URL_ERR_BADFILTER;
454 LDAP_FREE( ludp->lud_filter );
455 ludp->lud_filter = LDAP_STRDUP( p );
457 if( ludp->lud_filter == NULL ) {
459 ldap_free_urldesc( ludp );
460 return LDAP_URL_ERR_MEM;
468 return LDAP_URL_SUCCESS;
471 /* scan forward for '?' that may marks end of extensions */
473 q = strchr( p, '?' );
478 ldap_free_urldesc( ludp );
479 return LDAP_URL_ERR_BADURL;
482 /* parse the extensions */
483 ludp->lud_exts = ldap_str2charray( p, "," );
485 if( ludp->lud_exts == NULL ) {
487 ldap_free_urldesc( ludp );
488 return LDAP_URL_ERR_BADEXTS;
491 for( i=0; ludp->lud_exts[i] != NULL; i++ ) {
492 ldap_pvt_hex_unescape( ludp->lud_exts[i] );
496 /* must have 1 or more */
497 ldap_charray_free( ludp->lud_exts );
499 ldap_free_urldesc( ludp );
500 return LDAP_URL_ERR_BADEXTS;
506 return LDAP_URL_SUCCESS;
510 ldap_url_dup ( LDAPURLDesc *ludp )
514 if ( ludp == NULL ) {
518 dest = LDAP_MALLOC( sizeof(LDAPURLDesc) );
523 dest->lud_scheme = NULL;
524 dest->lud_host = NULL;
526 dest->lud_filter = NULL;
527 dest->lud_attrs = NULL;
528 dest->lud_exts = NULL;
529 dest->lud_next = NULL;
531 if ( ludp->lud_scheme != NULL ) {
532 dest->lud_scheme = LDAP_STRDUP( ludp->lud_scheme );
533 if (dest->lud_scheme == NULL) {
534 ldap_free_urldesc(dest);
539 if ( ludp->lud_host != NULL ) {
540 dest->lud_host = LDAP_STRDUP( ludp->lud_host );
541 if (dest->lud_host == NULL) {
542 ldap_free_urldesc(dest);
547 if ( ludp->lud_dn != NULL ) {
548 dest->lud_dn = LDAP_STRDUP( ludp->lud_dn );
549 if (dest->lud_dn == NULL) {
550 ldap_free_urldesc(dest);
555 if ( ludp->lud_filter != NULL ) {
556 dest->lud_filter = LDAP_STRDUP( ludp->lud_filter );
557 if (dest->lud_filter == NULL) {
558 ldap_free_urldesc(dest);
563 if ( ludp->lud_attrs != NULL ) {
564 dest->lud_attrs = ldap_charray_dup( ludp->lud_attrs );
565 if (dest->lud_attrs == NULL) {
566 ldap_free_urldesc(dest);
571 if ( ludp->lud_exts != NULL ) {
572 dest->lud_exts = ldap_charray_dup( ludp->lud_exts );
573 if (dest->lud_exts == NULL) {
574 ldap_free_urldesc(dest);
583 ldap_url_duplist (LDAPURLDesc *ludlist)
585 LDAPURLDesc *dest, *tail, *ludp, *newludp;
589 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
590 newludp = ldap_url_dup(ludp);
591 if (newludp == NULL) {
592 ldap_free_urllist(dest);
598 tail->lud_next = newludp;
605 ldap_url_parselist (LDAPURLDesc **ludlist, const char *url )
614 return LDAP_PARAM_ERROR;
616 urls = ldap_str2charray((char *)url, ", ");
618 return LDAP_NO_MEMORY;
620 /* count the URLs... */
621 for (i = 0; urls[i] != NULL; i++) ;
622 /* ...and put them in the "stack" backward */
624 rc = ldap_url_parse( urls[i], &ludp );
626 ldap_charray_free(urls);
627 ldap_free_urllist(*ludlist);
631 ludp->lud_next = *ludlist;
634 ldap_charray_free(urls);
639 ldap_url_parsehosts (LDAPURLDesc **ludlist, const char *hosts )
648 return LDAP_PARAM_ERROR;
650 specs = ldap_str2charray((char *)hosts, ", ");
652 return LDAP_NO_MEMORY;
654 /* count the URLs... */
655 for (i = 0; specs[i] != NULL; i++) /* EMPTY */;
657 /* ...and put them in the "stack" backward */
659 ludp = LDAP_CALLOC( 1, sizeof(LDAPURLDesc) );
661 ldap_charray_free(specs);
662 ldap_free_urllist(*ludlist);
664 return LDAP_NO_MEMORY;
666 ludp->lud_host = specs[i];
668 p = strchr(ludp->lud_host, ':');
671 ldap_pvt_hex_unescape(p);
672 ludp->lud_port = atoi(p);
674 ldap_pvt_hex_unescape(ludp->lud_host);
675 ludp->lud_scheme = LDAP_STRDUP("ldap");
676 ludp->lud_next = *ludlist;
680 /* this should be an array of NULLs now */
681 ldap_charray_free(specs);
686 ldap_url_list2hosts (LDAPURLDesc *ludlist)
690 char *s, *p, buf[32]; /* big enough to hold a long decimal # (overkill) */
695 /* figure out how big the string is */
696 size = 1; /* nul-term */
697 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
698 size += strlen(ludp->lud_host) + 1; /* host and space */
699 if (ludp->lud_port != 0)
700 size += sprintf(buf, ":%d", ludp->lud_port);
702 s = LDAP_MALLOC(size);
707 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
708 strcpy(p, ludp->lud_host);
709 p += strlen(ludp->lud_host);
710 if (ludp->lud_port != 0)
711 p += sprintf(p, ":%d", ludp->lud_port);
715 p--; /* nuke that extra space */
722 LDAPURLDesc *ludlist )
726 char *s, *p, buf[32]; /* big enough to hold a long decimal # (overkill) */
731 /* figure out how big the string is */
732 size = 1; /* nul-term */
733 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
734 size += strlen(ludp->lud_scheme) + strlen(ludp->lud_host);
735 size += sizeof(":/// ");
737 if (ludp->lud_port != 0) {
738 size += sprintf(buf, ":%d", ludp->lud_port);
742 s = LDAP_MALLOC(size);
748 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
749 p += sprintf(p, "%s://%s", ludp->lud_scheme, ludp->lud_host);
750 if (ludp->lud_port != 0)
751 p += sprintf(p, ":%d", ludp->lud_port);
756 p--; /* nuke that extra space */
762 ldap_free_urllist( LDAPURLDesc *ludlist )
764 LDAPURLDesc *ludp, *next;
766 for (ludp = ludlist; ludp != NULL; ludp = next) {
767 next = ludp->lud_next;
768 ldap_free_urldesc(ludp);
773 ldap_free_urldesc( LDAPURLDesc *ludp )
775 if ( ludp == NULL ) {
779 if ( ludp->lud_scheme != NULL ) {
780 LDAP_FREE( ludp->lud_scheme );
783 if ( ludp->lud_host != NULL ) {
784 LDAP_FREE( ludp->lud_host );
787 if ( ludp->lud_dn != NULL ) {
788 LDAP_FREE( ludp->lud_dn );
791 if ( ludp->lud_filter != NULL ) {
792 LDAP_FREE( ludp->lud_filter);
795 if ( ludp->lud_attrs != NULL ) {
796 LDAP_VFREE( ludp->lud_attrs );
799 if ( ludp->lud_exts != NULL ) {
800 LDAP_VFREE( ludp->lud_exts );
809 ldap_url_search( LDAP *ld, LDAP_CONST char *url, int attrsonly )
816 if ( ldap_url_parse( url, &ludp ) != 0 ) {
817 ld->ld_errno = LDAP_PARAM_ERROR;
821 ber = ldap_build_search_req( ld, ludp->lud_dn, ludp->lud_scope,
822 ludp->lud_filter, ludp->lud_attrs, attrsonly, NULL, NULL,
828 bind.ri_request = LDAP_REQ_SEARCH;
829 bind.ri_msgid = ld->ld_msgid;
830 bind.ri_url = (char *)url;
831 err = ldap_send_server_request(
832 ld, ber, ld->ld_msgid, NULL,
833 (ludp->lud_host != NULL || ludp->lud_port != 0)
838 ldap_free_urldesc( ludp );
844 ldap_url_search_st( LDAP *ld, LDAP_CONST char *url, int attrsonly,
845 struct timeval *timeout, LDAPMessage **res )
849 if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
850 return( ld->ld_errno );
853 if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 ) {
854 return( ld->ld_errno );
857 if ( ld->ld_errno == LDAP_TIMEOUT ) {
858 (void) ldap_abandon( ld, msgid );
859 ld->ld_errno = LDAP_TIMEOUT;
860 return( ld->ld_errno );
863 return( ldap_result2error( ld, *res, 0 ));
869 LDAP *ld, LDAP_CONST char *url, int attrsonly, LDAPMessage **res )
873 if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
874 return( ld->ld_errno );
877 if ( ldap_result( ld, msgid, 1, (struct timeval *)NULL, res ) == -1 ) {
878 return( ld->ld_errno );
881 return( ldap_result2error( ld, *res, 0 ));
886 ldap_pvt_hex_unescape( char *s )
889 * Remove URL hex escapes from s... done in place. The basic concept for
890 * this routine is borrowed from the WWW library HTUnEscape() routine.
894 for ( p = s; *s != '\0'; ++s ) {
896 if ( *++s != '\0' ) {
897 *p = ldap_pvt_unhex( *s ) << 4;
899 if ( *++s != '\0' ) {
900 *p++ += ldap_pvt_unhex( *s );
912 ldap_pvt_unhex( int c )
914 return( c >= '0' && c <= '9' ? c - '0'
915 : c >= 'A' && c <= 'F' ? c - 'A' + 10