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 url = LDAP_STRDUP( url_tmp );
252 return LDAP_URL_ERR_MEM;
256 p = &url[strlen(url)-1];
260 return LDAP_URL_ERR_BADENCLOSURE;
266 /* allocate return struct */
267 ludp = (LDAPURLDesc *)LDAP_CALLOC( 1, sizeof( LDAPURLDesc ));
269 if ( ludp == NULL ) {
271 return LDAP_URL_ERR_MEM;
274 ludp->lud_next = NULL;
275 ludp->lud_host = NULL;
278 ludp->lud_attrs = NULL;
279 ludp->lud_filter = NULL;
280 ludp->lud_scope = LDAP_SCOPE_BASE;
281 ludp->lud_filter = NULL;
283 ludp->lud_scheme = LDAP_STRDUP( scheme );
285 if ( ludp->lud_scheme == NULL ) {
287 ldap_free_urldesc( ludp );
288 return LDAP_URL_ERR_MEM;
291 /* scan forward for '/' that marks end of hostport and begin. of dn */
292 p = strchr( url, '/' );
295 /* terminate hostport; point to start of dn */
299 q = strchr( url, ':' );
303 ldap_pvt_hex_unescape( q );
307 ldap_free_urldesc( ludp );
308 return LDAP_URL_ERR_BADURL;
311 ludp->lud_port = atoi( q );
314 ldap_pvt_hex_unescape( url );
315 ludp->lud_host = LDAP_STRDUP( url );
317 if( ludp->lud_host == NULL ) {
319 ldap_free_urldesc( ludp );
320 return LDAP_URL_ERR_MEM;
324 * Kludge. ldap://111.222.333.444:389??cn=abc,o=company
326 * On early Novell releases, search references/referrals were returned
327 * in this format, i.e., the dn was kind of in the scope position,
328 * but the required slash is missing. The whole thing is illegal syntax,
329 * but we need to account for it. Fortunately it can't be confused with
332 if( (p == NULL) && (q != NULL) && ((q = strchr( q, '?')) != NULL)) {
334 /* ? immediately followed by question */
339 ldap_pvt_hex_unescape( q );
340 ludp->lud_dn = LDAP_STRDUP( q );
342 ludp->lud_dn = LDAP_STRDUP( "" );
345 if( ludp->lud_dn == NULL ) {
347 ldap_free_urldesc( ludp );
348 return LDAP_URL_ERR_MEM;
356 return LDAP_URL_SUCCESS;
359 /* scan forward for '?' that may marks end of dn */
360 q = strchr( p, '?' );
363 /* terminate dn part */
369 ldap_pvt_hex_unescape( p );
370 ludp->lud_dn = LDAP_STRDUP( p );
372 ludp->lud_dn = LDAP_STRDUP( "" );
375 if( ludp->lud_dn == NULL ) {
377 ldap_free_urldesc( ludp );
378 return LDAP_URL_ERR_MEM;
385 return LDAP_URL_SUCCESS;
388 /* scan forward for '?' that may marks end of attributes */
390 q = strchr( p, '?' );
393 /* terminate attributes part */
398 /* parse attributes */
399 ldap_pvt_hex_unescape( p );
400 ludp->lud_attrs = ldap_str2charray( p, "," );
402 if( ludp->lud_attrs == NULL ) {
404 ldap_free_urldesc( ludp );
405 return LDAP_URL_ERR_BADATTRS;
413 return LDAP_URL_SUCCESS;
416 /* scan forward for '?' that may marks end of scope */
418 q = strchr( p, '?' );
421 /* terminate the scope part */
426 /* parse the scope */
427 ldap_pvt_hex_unescape( p );
428 ludp->lud_scope = str2scope( p );
430 if( ludp->lud_scope == -1 ) {
432 ldap_free_urldesc( ludp );
433 return LDAP_URL_ERR_BADSCOPE;
441 return LDAP_URL_SUCCESS;
444 /* scan forward for '?' that may marks end of filter */
446 q = strchr( p, '?' );
449 /* terminate the filter part */
454 /* parse the filter */
455 ldap_pvt_hex_unescape( p );
460 ldap_free_urldesc( ludp );
461 return LDAP_URL_ERR_BADFILTER;
464 LDAP_FREE( ludp->lud_filter );
465 ludp->lud_filter = LDAP_STRDUP( p );
467 if( ludp->lud_filter == NULL ) {
469 ldap_free_urldesc( ludp );
470 return LDAP_URL_ERR_MEM;
478 return LDAP_URL_SUCCESS;
481 /* scan forward for '?' that may marks end of extensions */
483 q = strchr( p, '?' );
488 ldap_free_urldesc( ludp );
489 return LDAP_URL_ERR_BADURL;
492 /* parse the extensions */
493 ludp->lud_exts = ldap_str2charray( p, "," );
495 if( ludp->lud_exts == NULL ) {
497 ldap_free_urldesc( ludp );
498 return LDAP_URL_ERR_BADEXTS;
501 for( i=0; ludp->lud_exts[i] != NULL; i++ ) {
502 ldap_pvt_hex_unescape( ludp->lud_exts[i] );
506 /* must have 1 or more */
507 ldap_charray_free( ludp->lud_exts );
509 ldap_free_urldesc( ludp );
510 return LDAP_URL_ERR_BADEXTS;
516 return LDAP_URL_SUCCESS;
520 ldap_url_dup ( LDAPURLDesc *ludp )
524 if ( ludp == NULL ) {
528 dest = LDAP_MALLOC( sizeof(LDAPURLDesc) );
533 dest->lud_scheme = NULL;
534 dest->lud_host = NULL;
536 dest->lud_filter = NULL;
537 dest->lud_attrs = NULL;
538 dest->lud_exts = NULL;
539 dest->lud_next = NULL;
541 if ( ludp->lud_scheme != NULL ) {
542 dest->lud_scheme = LDAP_STRDUP( ludp->lud_scheme );
543 if (dest->lud_scheme == NULL) {
544 ldap_free_urldesc(dest);
549 if ( ludp->lud_host != NULL ) {
550 dest->lud_host = LDAP_STRDUP( ludp->lud_host );
551 if (dest->lud_host == NULL) {
552 ldap_free_urldesc(dest);
557 if ( ludp->lud_dn != NULL ) {
558 dest->lud_dn = LDAP_STRDUP( ludp->lud_dn );
559 if (dest->lud_dn == NULL) {
560 ldap_free_urldesc(dest);
565 if ( ludp->lud_filter != NULL ) {
566 dest->lud_filter = LDAP_STRDUP( ludp->lud_filter );
567 if (dest->lud_filter == NULL) {
568 ldap_free_urldesc(dest);
573 if ( ludp->lud_attrs != NULL ) {
574 dest->lud_attrs = ldap_charray_dup( ludp->lud_attrs );
575 if (dest->lud_attrs == NULL) {
576 ldap_free_urldesc(dest);
581 if ( ludp->lud_exts != NULL ) {
582 dest->lud_exts = ldap_charray_dup( ludp->lud_exts );
583 if (dest->lud_exts == NULL) {
584 ldap_free_urldesc(dest);
593 ldap_url_duplist (LDAPURLDesc *ludlist)
595 LDAPURLDesc *dest, *tail, *ludp, *newludp;
599 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
600 newludp = ldap_url_dup(ludp);
601 if (newludp == NULL) {
602 ldap_free_urllist(dest);
608 tail->lud_next = newludp;
615 ldap_url_parselist (LDAPURLDesc **ludlist, const char *url )
624 return LDAP_PARAM_ERROR;
626 urls = ldap_str2charray((char *)url, ", ");
628 return LDAP_NO_MEMORY;
630 /* count the URLs... */
631 for (i = 0; urls[i] != NULL; i++) ;
632 /* ...and put them in the "stack" backward */
634 rc = ldap_url_parse( urls[i], &ludp );
636 ldap_charray_free(urls);
637 ldap_free_urllist(*ludlist);
641 ludp->lud_next = *ludlist;
644 ldap_charray_free(urls);
649 ldap_url_parsehosts (LDAPURLDesc **ludlist, const char *hosts )
658 return LDAP_PARAM_ERROR;
660 specs = ldap_str2charray((char *)hosts, ", ");
662 return LDAP_NO_MEMORY;
664 /* count the URLs... */
665 for (i = 0; specs[i] != NULL; i++) /* EMPTY */;
667 /* ...and put them in the "stack" backward */
669 ludp = LDAP_CALLOC( 1, sizeof(LDAPURLDesc) );
671 ldap_charray_free(specs);
672 ldap_free_urllist(*ludlist);
674 return LDAP_NO_MEMORY;
676 ludp->lud_host = specs[i];
678 p = strchr(ludp->lud_host, ':');
681 ldap_pvt_hex_unescape(p);
682 ludp->lud_port = atoi(p);
684 ldap_pvt_hex_unescape(ludp->lud_host);
685 ludp->lud_scheme = LDAP_STRDUP("ldap");
686 ludp->lud_next = *ludlist;
690 /* this should be an array of NULLs now */
691 ldap_charray_free(specs);
696 ldap_url_list2hosts (LDAPURLDesc *ludlist)
700 char *s, *p, buf[32]; /* big enough to hold a long decimal # (overkill) */
705 /* figure out how big the string is */
706 size = 1; /* nul-term */
707 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
708 size += strlen(ludp->lud_host) + 1; /* host and space */
709 if (ludp->lud_port != 0)
710 size += sprintf(buf, ":%d", ludp->lud_port);
712 s = LDAP_MALLOC(size);
717 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
718 strcpy(p, ludp->lud_host);
719 p += strlen(ludp->lud_host);
720 if (ludp->lud_port != 0)
721 p += sprintf(p, ":%d", ludp->lud_port);
725 p--; /* nuke that extra space */
732 LDAPURLDesc *ludlist )
736 char *s, *p, buf[32]; /* big enough to hold a long decimal # (overkill) */
741 /* figure out how big the string is */
742 size = 1; /* nul-term */
743 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
744 size += strlen(ludp->lud_scheme) + strlen(ludp->lud_host);
745 size += sizeof(":/// ");
747 if (ludp->lud_port != 0) {
748 size += sprintf(buf, ":%d", ludp->lud_port);
752 s = LDAP_MALLOC(size);
758 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
759 p += sprintf(p, "%s://%s", ludp->lud_scheme, ludp->lud_host);
760 if (ludp->lud_port != 0)
761 p += sprintf(p, ":%d", ludp->lud_port);
766 p--; /* nuke that extra space */
772 ldap_free_urllist( LDAPURLDesc *ludlist )
774 LDAPURLDesc *ludp, *next;
776 for (ludp = ludlist; ludp != NULL; ludp = next) {
777 next = ludp->lud_next;
778 ldap_free_urldesc(ludp);
783 ldap_free_urldesc( LDAPURLDesc *ludp )
785 if ( ludp == NULL ) {
789 if ( ludp->lud_scheme != NULL ) {
790 LDAP_FREE( ludp->lud_scheme );
793 if ( ludp->lud_host != NULL ) {
794 LDAP_FREE( ludp->lud_host );
797 if ( ludp->lud_dn != NULL ) {
798 LDAP_FREE( ludp->lud_dn );
801 if ( ludp->lud_filter != NULL ) {
802 LDAP_FREE( ludp->lud_filter);
805 if ( ludp->lud_attrs != NULL ) {
806 LDAP_VFREE( ludp->lud_attrs );
809 if ( ludp->lud_exts != NULL ) {
810 LDAP_VFREE( ludp->lud_exts );
819 ldap_url_search( LDAP *ld, LDAP_CONST char *url, int attrsonly )
826 if ( ldap_url_parse( url, &ludp ) != 0 ) {
827 ld->ld_errno = LDAP_PARAM_ERROR;
831 ber = ldap_build_search_req( ld, ludp->lud_dn, ludp->lud_scope,
832 ludp->lud_filter, ludp->lud_attrs, attrsonly, NULL, NULL,
838 bind.ri_request = LDAP_REQ_SEARCH;
839 bind.ri_msgid = ld->ld_msgid;
840 bind.ri_url = (char *)url;
841 err = ldap_send_server_request(
842 ld, ber, ld->ld_msgid, NULL,
843 (ludp->lud_host != NULL || ludp->lud_port != 0)
848 ldap_free_urldesc( ludp );
854 ldap_url_search_st( LDAP *ld, LDAP_CONST char *url, int attrsonly,
855 struct timeval *timeout, LDAPMessage **res )
859 if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
860 return( ld->ld_errno );
863 if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 ) {
864 return( ld->ld_errno );
867 if ( ld->ld_errno == LDAP_TIMEOUT ) {
868 (void) ldap_abandon( ld, msgid );
869 ld->ld_errno = LDAP_TIMEOUT;
870 return( ld->ld_errno );
873 return( ldap_result2error( ld, *res, 0 ));
879 LDAP *ld, LDAP_CONST char *url, int attrsonly, LDAPMessage **res )
883 if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
884 return( ld->ld_errno );
887 if ( ldap_result( ld, msgid, 1, (struct timeval *)NULL, res ) == -1 ) {
888 return( ld->ld_errno );
891 return( ldap_result2error( ld, *res, 0 ));
896 ldap_pvt_hex_unescape( char *s )
899 * Remove URL hex escapes from s... done in place. The basic concept for
900 * this routine is borrowed from the WWW library HTUnEscape() routine.
904 for ( p = s; *s != '\0'; ++s ) {
906 if ( *++s != '\0' ) {
907 *p = ldap_pvt_unhex( *s ) << 4;
909 if ( *++s != '\0' ) {
910 *p++ += ldap_pvt_unhex( *s );
922 ldap_pvt_unhex( int c )
924 return( c >= '0' && c <= '9' ? c - '0'
925 : c >= 'A' && c <= 'F' ? c - 'A' + 10