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>
31 #include <ac/socket.h>
32 #include <ac/string.h>
39 static const char* skip_url_prefix LDAP_P((
42 const char **scheme ));
44 int ldap_pvt_url_scheme2proto( const char *scheme )
48 if( scheme == NULL ) {
52 if( strcmp("ldap", scheme) == 0 ) {
53 return LDAP_PROTO_TCP;
56 if( strcmp("ldapi", scheme) == 0 ) {
57 return LDAP_PROTO_IPC;
60 if( strcmp("ldaps", scheme) == 0 ) {
61 return LDAP_PROTO_TCP;
67 int ldap_pvt_url_scheme2tls( const char *scheme )
71 if( scheme == NULL ) {
75 return strcmp("ldaps", scheme) == 0;
79 ldap_is_ldap_url( LDAP_CONST char *url )
88 if( skip_url_prefix( url, &enclosed, &scheme ) == NULL ) {
96 ldap_is_ldaps_url( LDAP_CONST char *url )
105 if( skip_url_prefix( url, &enclosed, &scheme ) == NULL ) {
109 return strcmp(scheme, "ldaps") == 0;
113 ldap_is_ldapi_url( LDAP_CONST char *url )
122 if( skip_url_prefix( url, &enclosed, &scheme ) == NULL ) {
126 return strcmp(scheme, "ldapi") == 0;
133 const char **scheme )
136 * return non-zero if this looks like a LDAP URL; zero if not
137 * if non-zero returned, *urlp will be moved past "ldap://" part of URL
147 /* skip leading '<' (if any) */
155 /* skip leading "URL:" (if any) */
156 if ( strncasecmp( p, LDAP_URL_URLCOLON, LDAP_URL_URLCOLON_LEN ) == 0 ) {
157 p += LDAP_URL_URLCOLON_LEN;
160 /* check for "ldap://" prefix */
161 if ( strncasecmp( p, LDAP_URL_PREFIX, LDAP_URL_PREFIX_LEN ) == 0 ) {
162 /* skip over "ldap://" prefix and return success */
163 p += LDAP_URL_PREFIX_LEN;
168 /* check for "ldaps://" prefix */
169 if ( strncasecmp( p, LDAPS_URL_PREFIX, LDAPS_URL_PREFIX_LEN ) == 0 ) {
170 /* skip over "ldaps://" prefix and return success */
171 p += LDAPS_URL_PREFIX_LEN;
176 /* check for "ldapi://" prefix */
177 if ( strncasecmp( p, LDAPI_URL_PREFIX, LDAPI_URL_PREFIX_LEN ) == 0 ) {
178 /* skip over "ldapi://" prefix and return success */
179 p += LDAPI_URL_PREFIX_LEN;
188 static int str2scope( const char *p )
190 if ( strcasecmp( p, "one" ) == 0 ) {
191 return LDAP_SCOPE_ONELEVEL;
193 } else if ( strcasecmp( p, "onetree" ) == 0 ) {
194 return LDAP_SCOPE_ONELEVEL;
196 } else if ( strcasecmp( p, "base" ) == 0 ) {
197 return LDAP_SCOPE_BASE;
199 } else if ( strcasecmp( p, "sub" ) == 0 ) {
200 return LDAP_SCOPE_SUBTREE;
202 } else if ( strcasecmp( p, "subtree" ) == 0 ) {
203 return LDAP_SCOPE_SUBTREE;
211 ldap_url_parse( LDAP_CONST char *url_in, LDAPURLDesc **ludpp )
214 * Pick apart the pieces of an LDAP URL.
220 const char *scheme = NULL;
224 if( url_in == NULL && ludpp == NULL ) {
225 return LDAP_URL_ERR_PARAM;
228 #ifndef LDAP_INT_IN_KERNEL
229 /* Global options may not be created yet
230 * We can't test if the global options are initialized
231 * because a call to LDAP_INT_GLOBAL_OPT() will try to allocate
232 * the options and cause infinite recursion
234 Debug( LDAP_DEBUG_TRACE, "ldap_url_parse(%s)\n", url_in, 0, 0 );
237 *ludpp = NULL; /* pessimistic */
239 url_tmp = skip_url_prefix( url_in, &enclosed, &scheme );
241 if ( url_tmp == NULL ) {
242 return LDAP_URL_ERR_BADSCHEME;
247 /* make working copy of the remainder of the URL */
248 url = LDAP_STRDUP( url_tmp );
250 return LDAP_URL_ERR_MEM;
254 p = &url[strlen(url)-1];
258 return LDAP_URL_ERR_BADENCLOSURE;
264 /* allocate return struct */
265 ludp = (LDAPURLDesc *)LDAP_CALLOC( 1, sizeof( LDAPURLDesc ));
267 if ( ludp == NULL ) {
269 return LDAP_URL_ERR_MEM;
272 ludp->lud_next = NULL;
273 ludp->lud_host = NULL;
274 ludp->lud_port = LDAP_PORT;
276 ludp->lud_attrs = NULL;
277 ludp->lud_filter = NULL;
278 ludp->lud_scope = LDAP_SCOPE_BASE;
279 ludp->lud_filter = NULL;
281 ludp->lud_scheme = LDAP_STRDUP( scheme );
283 if ( ludp->lud_scheme == NULL ) {
285 ldap_free_urldesc( ludp );
286 return LDAP_URL_ERR_MEM;
289 if( strcasecmp( ludp->lud_scheme, "ldaps" ) == 0 ) {
290 ludp->lud_port = LDAPS_PORT;
293 /* scan forward for '/' that marks end of hostport and begin. of dn */
294 p = strchr( url, '/' );
297 /* terminate hostport; point to start of dn */
301 /* IPv6 syntax with [ip address]:port */
303 r = strchr( url, ']' );
306 ldap_free_urldesc( ludp );
307 return LDAP_URL_ERR_BADURL;
310 q = strchr( r, ':' );
312 q = strchr( url, ':' );
317 ldap_pvt_hex_unescape( q );
321 ldap_free_urldesc( ludp );
322 return LDAP_URL_ERR_BADURL;
325 ludp->lud_port = atoi( q );
328 ldap_pvt_hex_unescape( url );
330 /* If [ip address]:port syntax, url is [ip and we skip the [ */
331 ludp->lud_host = LDAP_STRDUP( url + ( *url == '[' ) );
333 if( ludp->lud_host == NULL ) {
335 ldap_free_urldesc( ludp );
336 return LDAP_URL_ERR_MEM;
340 * Kludge. ldap://111.222.333.444:389??cn=abc,o=company
342 * On early Novell releases, search references/referrals were returned
343 * in this format, i.e., the dn was kind of in the scope position,
344 * but the required slash is missing. The whole thing is illegal syntax,
345 * but we need to account for it. Fortunately it can't be confused with
348 if( (p == NULL) && (q != NULL) && ((q = strchr( q, '?')) != NULL)) {
350 /* ? immediately followed by question */
355 ldap_pvt_hex_unescape( q );
356 ludp->lud_dn = LDAP_STRDUP( q );
358 ludp->lud_dn = LDAP_STRDUP( "" );
361 if( ludp->lud_dn == NULL ) {
363 ldap_free_urldesc( ludp );
364 return LDAP_URL_ERR_MEM;
372 return LDAP_URL_SUCCESS;
375 /* scan forward for '?' that may marks end of dn */
376 q = strchr( p, '?' );
379 /* terminate dn part */
385 ldap_pvt_hex_unescape( p );
386 ludp->lud_dn = LDAP_STRDUP( p );
388 ludp->lud_dn = LDAP_STRDUP( "" );
391 if( ludp->lud_dn == NULL ) {
393 ldap_free_urldesc( ludp );
394 return LDAP_URL_ERR_MEM;
401 return LDAP_URL_SUCCESS;
404 /* scan forward for '?' that may marks end of attributes */
406 q = strchr( p, '?' );
409 /* terminate attributes part */
414 /* parse attributes */
415 ldap_pvt_hex_unescape( p );
416 ludp->lud_attrs = ldap_str2charray( p, "," );
418 if( ludp->lud_attrs == NULL ) {
420 ldap_free_urldesc( ludp );
421 return LDAP_URL_ERR_BADATTRS;
429 return LDAP_URL_SUCCESS;
432 /* scan forward for '?' that may marks end of scope */
434 q = strchr( p, '?' );
437 /* terminate the scope part */
442 /* parse the scope */
443 ldap_pvt_hex_unescape( p );
444 ludp->lud_scope = str2scope( p );
446 if( ludp->lud_scope == -1 ) {
448 ldap_free_urldesc( ludp );
449 return LDAP_URL_ERR_BADSCOPE;
457 return LDAP_URL_SUCCESS;
460 /* scan forward for '?' that may marks end of filter */
462 q = strchr( p, '?' );
465 /* terminate the filter part */
470 /* parse the filter */
471 ldap_pvt_hex_unescape( p );
476 ldap_free_urldesc( ludp );
477 return LDAP_URL_ERR_BADFILTER;
480 LDAP_FREE( ludp->lud_filter );
481 ludp->lud_filter = LDAP_STRDUP( p );
483 if( ludp->lud_filter == NULL ) {
485 ldap_free_urldesc( ludp );
486 return LDAP_URL_ERR_MEM;
494 return LDAP_URL_SUCCESS;
497 /* scan forward for '?' that may marks end of extensions */
499 q = strchr( p, '?' );
504 ldap_free_urldesc( ludp );
505 return LDAP_URL_ERR_BADURL;
508 /* parse the extensions */
509 ludp->lud_exts = ldap_str2charray( p, "," );
511 if( ludp->lud_exts == NULL ) {
513 ldap_free_urldesc( ludp );
514 return LDAP_URL_ERR_BADEXTS;
517 for( i=0; ludp->lud_exts[i] != NULL; i++ ) {
518 ldap_pvt_hex_unescape( ludp->lud_exts[i] );
522 /* must have 1 or more */
523 ldap_charray_free( ludp->lud_exts );
525 ldap_free_urldesc( ludp );
526 return LDAP_URL_ERR_BADEXTS;
532 return LDAP_URL_SUCCESS;
536 ldap_url_dup ( LDAPURLDesc *ludp )
540 if ( ludp == NULL ) {
544 dest = LDAP_MALLOC( sizeof(LDAPURLDesc) );
549 dest->lud_scheme = NULL;
550 dest->lud_host = NULL;
552 dest->lud_filter = NULL;
553 dest->lud_attrs = NULL;
554 dest->lud_exts = NULL;
555 dest->lud_next = NULL;
557 if ( ludp->lud_scheme != NULL ) {
558 dest->lud_scheme = LDAP_STRDUP( ludp->lud_scheme );
559 if (dest->lud_scheme == NULL) {
560 ldap_free_urldesc(dest);
565 if ( ludp->lud_host != NULL ) {
566 dest->lud_host = LDAP_STRDUP( ludp->lud_host );
567 if (dest->lud_host == NULL) {
568 ldap_free_urldesc(dest);
573 if ( ludp->lud_dn != NULL ) {
574 dest->lud_dn = LDAP_STRDUP( ludp->lud_dn );
575 if (dest->lud_dn == NULL) {
576 ldap_free_urldesc(dest);
581 if ( ludp->lud_filter != NULL ) {
582 dest->lud_filter = LDAP_STRDUP( ludp->lud_filter );
583 if (dest->lud_filter == NULL) {
584 ldap_free_urldesc(dest);
589 if ( ludp->lud_attrs != NULL ) {
590 dest->lud_attrs = ldap_charray_dup( ludp->lud_attrs );
591 if (dest->lud_attrs == NULL) {
592 ldap_free_urldesc(dest);
597 if ( ludp->lud_exts != NULL ) {
598 dest->lud_exts = ldap_charray_dup( ludp->lud_exts );
599 if (dest->lud_exts == NULL) {
600 ldap_free_urldesc(dest);
609 ldap_url_duplist (LDAPURLDesc *ludlist)
611 LDAPURLDesc *dest, *tail, *ludp, *newludp;
615 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
616 newludp = ldap_url_dup(ludp);
617 if (newludp == NULL) {
618 ldap_free_urllist(dest);
624 tail->lud_next = newludp;
631 ldap_url_parselist (LDAPURLDesc **ludlist, const char *url )
640 return LDAP_PARAM_ERROR;
642 urls = ldap_str2charray((char *)url, ", ");
644 return LDAP_NO_MEMORY;
646 /* count the URLs... */
647 for (i = 0; urls[i] != NULL; i++) ;
648 /* ...and put them in the "stack" backward */
650 rc = ldap_url_parse( urls[i], &ludp );
652 ldap_charray_free(urls);
653 ldap_free_urllist(*ludlist);
657 ludp->lud_next = *ludlist;
660 ldap_charray_free(urls);
666 LDAPURLDesc **ludlist,
677 return LDAP_PARAM_ERROR;
679 specs = ldap_str2charray((char *)hosts, ", ");
681 return LDAP_NO_MEMORY;
683 /* count the URLs... */
684 for (i = 0; specs[i] != NULL; i++) /* EMPTY */;
686 /* ...and put them in the "stack" backward */
688 ludp = LDAP_CALLOC( 1, sizeof(LDAPURLDesc) );
690 ldap_charray_free(specs);
691 ldap_free_urllist(*ludlist);
693 return LDAP_NO_MEMORY;
695 ludp->lud_port = port;
696 ludp->lud_host = specs[i];
698 p = strchr(ludp->lud_host, ':');
700 /* more than one :, IPv6 address */
701 if ( strchr(p+1, ':') != NULL ) {
702 /* allow [address] and [address]:port */
703 if ( *ludp->lud_host == '[' ) {
704 p = LDAP_STRDUP(ludp->lud_host+1);
705 /* copied, make sure we free source later */
706 specs[i] = ludp->lud_host;
708 p = strchr( ludp->lud_host, ']' );
710 return LDAP_PARAM_ERROR;
714 return LDAP_PARAM_ERROR;
723 ldap_pvt_hex_unescape(p);
724 ludp->lud_port = atoi(p);
727 ldap_pvt_hex_unescape(ludp->lud_host);
728 ludp->lud_scheme = LDAP_STRDUP("ldap");
729 ludp->lud_next = *ludlist;
733 /* this should be an array of NULLs now */
734 /* except entries starting with [ */
735 ldap_charray_free(specs);
740 ldap_url_list2hosts (LDAPURLDesc *ludlist)
744 char *s, *p, buf[32]; /* big enough to hold a long decimal # (overkill) */
749 /* figure out how big the string is */
750 size = 1; /* nul-term */
751 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
752 size += strlen(ludp->lud_host) + 1; /* host and space */
753 if (strchr(ludp->lud_host, ':')) /* will add [ ] below */
755 if (ludp->lud_port != 0)
756 size += sprintf(buf, ":%d", ludp->lud_port);
758 s = LDAP_MALLOC(size);
763 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
764 if (strchr(ludp->lud_host, ':')) {
765 p += sprintf(p, "[%s]", ludp->lud_host);
767 strcpy(p, ludp->lud_host);
768 p += strlen(ludp->lud_host);
770 if (ludp->lud_port != 0)
771 p += sprintf(p, ":%d", ludp->lud_port);
775 p--; /* nuke that extra space */
782 LDAPURLDesc *ludlist )
786 char *s, *p, buf[32]; /* big enough to hold a long decimal # (overkill) */
791 /* figure out how big the string is */
792 size = 1; /* nul-term */
793 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
794 size += strlen(ludp->lud_scheme) + strlen(ludp->lud_host);
795 if (strchr(ludp->lud_host, ':')) /* will add [ ] below */
797 size += sizeof(":/// ");
799 if (ludp->lud_port != 0) {
800 size += sprintf(buf, ":%d", ludp->lud_port);
804 s = LDAP_MALLOC(size);
810 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
812 strchr(ludp->lud_host, ':') ? "%s://[%s]" : "%s://%s",
813 ludp->lud_scheme, ludp->lud_host);
814 if (ludp->lud_port != 0)
815 p += sprintf(p, ":%d", ludp->lud_port);
820 p--; /* nuke that extra space */
826 ldap_free_urllist( LDAPURLDesc *ludlist )
828 LDAPURLDesc *ludp, *next;
830 for (ludp = ludlist; ludp != NULL; ludp = next) {
831 next = ludp->lud_next;
832 ldap_free_urldesc(ludp);
837 ldap_free_urldesc( LDAPURLDesc *ludp )
839 if ( ludp == NULL ) {
843 if ( ludp->lud_scheme != NULL ) {
844 LDAP_FREE( ludp->lud_scheme );
847 if ( ludp->lud_host != NULL ) {
848 LDAP_FREE( ludp->lud_host );
851 if ( ludp->lud_dn != NULL ) {
852 LDAP_FREE( ludp->lud_dn );
855 if ( ludp->lud_filter != NULL ) {
856 LDAP_FREE( ludp->lud_filter);
859 if ( ludp->lud_attrs != NULL ) {
860 LDAP_VFREE( ludp->lud_attrs );
863 if ( ludp->lud_exts != NULL ) {
864 LDAP_VFREE( ludp->lud_exts );
873 ldap_url_search( LDAP *ld, LDAP_CONST char *url, int attrsonly )
880 if ( ldap_url_parse( url, &ludp ) != 0 ) {
881 ld->ld_errno = LDAP_PARAM_ERROR;
885 ber = ldap_build_search_req( ld, ludp->lud_dn, ludp->lud_scope,
886 ludp->lud_filter, ludp->lud_attrs, attrsonly, NULL, NULL,
892 bind.ri_request = LDAP_REQ_SEARCH;
893 bind.ri_msgid = ld->ld_msgid;
894 bind.ri_url = (char *)url;
895 err = ldap_send_server_request(
896 ld, ber, ld->ld_msgid, NULL,
900 ldap_free_urldesc( ludp );
906 ldap_url_search_st( LDAP *ld, LDAP_CONST char *url, int attrsonly,
907 struct timeval *timeout, LDAPMessage **res )
911 if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
912 return( ld->ld_errno );
915 if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 ) {
916 return( ld->ld_errno );
919 if ( ld->ld_errno == LDAP_TIMEOUT ) {
920 (void) ldap_abandon( ld, msgid );
921 ld->ld_errno = LDAP_TIMEOUT;
922 return( ld->ld_errno );
925 return( ldap_result2error( ld, *res, 0 ));
931 LDAP *ld, LDAP_CONST char *url, int attrsonly, LDAPMessage **res )
935 if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
936 return( ld->ld_errno );
939 if ( ldap_result( ld, msgid, 1, (struct timeval *)NULL, res ) == -1 ) {
940 return( ld->ld_errno );
943 return( ldap_result2error( ld, *res, 0 ));
948 ldap_pvt_hex_unescape( char *s )
951 * Remove URL hex escapes from s... done in place. The basic concept for
952 * this routine is borrowed from the WWW library HTUnEscape() routine.
956 for ( p = s; *s != '\0'; ++s ) {
958 if ( *++s != '\0' ) {
959 *p = ldap_pvt_unhex( *s ) << 4;
961 if ( *++s != '\0' ) {
962 *p++ += ldap_pvt_unhex( *s );
974 ldap_pvt_unhex( int c )
976 return( c >= '0' && c <= '9' ? c - '0'
977 : c >= 'A' && c <= 'F' ? c - 'A' + 10