2 * Copyright 2001-2003 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
6 #pragma ident "%Z%%M% %I% %E% SMI"
10 * The contents of this file are subject to the Netscape Public
11 * License Version 1.1 (the "License"); you may not use this file
12 * except in compliance with the License. You may obtain a copy of
13 * the License at http://www.mozilla.org/NPL/
15 * Software distributed under the License is distributed on an "AS
16 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
17 * implied. See the License for the specific language governing
18 * rights and limitations under the License.
20 * The Original Code is Mozilla Communicator client code, released
23 * The Initial Developer of the Original Code is Netscape
24 * Communications Corporation. Portions created by Netscape are
25 * Copyright (C) 1998-1999 Netscape Communications Corporation. All
31 * Copyright (c) 1996 Regents of the University of Michigan.
32 * All rights reserved.
35 /* LIBLDAP url.c -- LDAP URL related routines
37 * LDAP URLs look like this:
38 * l d a p : / / hostport / dn [ ? attributes [ ? scope [ ? filter ] ] ]
41 * attributes is a comma separated list
42 * scope is one of these three strings: base one sub (default=base)
43 * filter is an string-represented filter as in RFC 1558
45 * e.g., ldap://ldap.itd.umich.edu/c=US?o,description?one?o=umich
47 * We also tolerate URLs that look like: <ldapurl> and <URL:ldapurl>
52 static char copyright
[] = "@(#) Copyright (c) 1996 Regents of the University of Michigan.\nAll rights reserved.\n";
59 static int skip_url_prefix( const char **urlp
, int *enclosedp
, int *securep
);
64 ldap_is_ldap_url( const char *url
)
69 && skip_url_prefix( &url
, &enclosed
, &secure
));
74 skip_url_prefix( const char **urlp
, int *enclosedp
, int *securep
)
77 * return non-zero if this looks like a LDAP URL; zero if not
78 * if non-zero returned, *urlp will be moved past "ldap://" part of URL
79 * The data that *urlp points to is not changed by this function.
81 if ( *urlp
== NULL
) {
85 /* skip leading '<' (if any) */
86 if ( **urlp
== '<' ) {
93 /* skip leading "URL:" (if any) */
94 if ( strlen( *urlp
) >= LDAP_URL_URLCOLON_LEN
&& strncasecmp(
95 *urlp
, LDAP_URL_URLCOLON
, LDAP_URL_URLCOLON_LEN
) == 0 ) {
96 *urlp
+= LDAP_URL_URLCOLON_LEN
;
99 /* check for an "ldap://" prefix */
100 if ( strlen( *urlp
) >= LDAP_URL_PREFIX_LEN
&& strncasecmp( *urlp
,
101 LDAP_URL_PREFIX
, LDAP_URL_PREFIX_LEN
) == 0 ) {
102 /* skip over URL prefix and return success */
103 *urlp
+= LDAP_URL_PREFIX_LEN
;
108 /* check for an "ldaps://" prefix */
109 if ( strlen( *urlp
) >= LDAPS_URL_PREFIX_LEN
&& strncasecmp( *urlp
,
110 LDAPS_URL_PREFIX
, LDAPS_URL_PREFIX_LEN
) == 0 ) {
111 /* skip over URL prefix and return success */
112 *urlp
+= LDAPS_URL_PREFIX_LEN
;
117 return( 0 ); /* not an LDAP URL */
123 ldap_url_parse( const char *url
, LDAPURLDesc
**ludpp
)
126 * Pick apart the pieces of an LDAP URL.
130 if (( rc
= nsldapi_url_parse( url
, ludpp
, 1 )) == 0 ) {
131 if ( (*ludpp
)->lud_scope
== -1 ) {
132 (*ludpp
)->lud_scope
= LDAP_SCOPE_BASE
;
134 if ( (*ludpp
)->lud_filter
== NULL
) {
135 (*ludpp
)->lud_filter
= "(objectclass=*)";
137 if ( *((*ludpp
)->lud_dn
) == '\0' ) {
138 (*ludpp
)->lud_dn
= NULL
;
145 /* same as ldap_url_parse(), but dn is not require */
148 ldap_url_parse_nodn(const char *url
, LDAPURLDesc
**ludpp
)
151 * Pick apart the pieces of an LDAP URL.
155 if ((rc
= nsldapi_url_parse(url
, ludpp
, 0)) == 0) {
156 if ((*ludpp
)->lud_scope
== -1) {
157 (*ludpp
)->lud_scope
= LDAP_SCOPE_BASE
;
159 if ((*ludpp
)->lud_filter
== NULL
) {
160 (*ludpp
)->lud_filter
= "(objectclass=*)";
162 if ((*ludpp
)->lud_dn
&& *((*ludpp
)->lud_dn
) == '\0') {
163 (*ludpp
)->lud_dn
= NULL
;
172 * like ldap_url_parse() with a few exceptions:
173 * 1) if dn_required is zero, a missing DN does not generate an error
174 * (we just leave the lud_dn field NULL)
175 * 2) no defaults are set for lud_scope and lud_filter (they are set to -1
176 * and NULL respectively if no SCOPE or FILTER are present in the URL).
177 * 3) when there is a zero-length DN in a URL we do not set lud_dn to NULL.
178 * 4) if an LDAPv3 URL extensions are included,
181 nsldapi_url_parse( const char *url
, LDAPURLDesc
**ludpp
, int dn_required
)
185 char *urlcopy
, *attrs
, *scope
, *extensions
= NULL
, *p
, *q
;
186 int enclosed
, secure
, i
, nattrs
, at_start
;
188 LDAPDebug( LDAP_DEBUG_TRACE
, "nsldapi_url_parse(%s)\n", url
, 0, 0 );
190 if ( url
== NULL
|| ludpp
== NULL
) {
191 return( LDAP_URL_ERR_PARAM
);
194 *ludpp
= NULL
; /* pessimistic */
196 if ( !skip_url_prefix( &url
, &enclosed
, &secure
)) {
197 return( LDAP_URL_ERR_NOTLDAP
);
200 /* allocate return struct */
201 if (( ludp
= (LDAPURLDesc
*)NSLDAPI_CALLOC( 1, sizeof( LDAPURLDesc
)))
202 == NULLLDAPURLDESC
) {
203 return( LDAP_URL_ERR_MEM
);
207 ludp
->lud_options
|= LDAP_URL_OPT_SECURE
;
210 /* make working copy of the remainder of the URL */
211 if (( urlcopy
= nsldapi_strdup( url
)) == NULL
) {
212 ldap_free_urldesc( ludp
);
213 return( LDAP_URL_ERR_MEM
);
216 if ( enclosed
&& *((p
= urlcopy
+ strlen( urlcopy
) - 1)) == '>' ) {
220 /* initialize scope and filter */
221 ludp
->lud_scope
= -1;
222 ludp
->lud_filter
= NULL
;
224 /* lud_string is the only malloc'd string space we use */
225 ludp
->lud_string
= urlcopy
;
227 /* scan forward for '/' that marks end of hostport and begin. of dn */
228 if (( ludp
->lud_dn
= strchr( urlcopy
, '/' )) == NULL
) {
230 ldap_free_urldesc( ludp
);
231 return( LDAP_URL_ERR_NODN
);
234 /* terminate hostport; point to start of dn */
235 *ludp
->lud_dn
++ = '\0';
239 if ( *urlcopy
== '\0' ) {
240 ludp
->lud_host
= NULL
;
242 ludp
->lud_host
= urlcopy
;
243 nsldapi_hex_unescape( ludp
->lud_host
);
246 * Locate and strip off optional port number (:#) in host
249 * If more than one space-separated host is listed, we only
250 * look for a port number within the right-most one since
251 * ldap_init() will handle host parameters that look like
254 if (( p
= strrchr( ludp
->lud_host
, ' ' )) == NULL
) {
259 if ( *p
== '[' && ( q
= strchr( p
, ']' )) != NULL
) {
260 /* square brackets present -- skip past them */
263 if (( p
= strchr( p
, ':' )) != NULL
) {
265 ludp
->lud_port
= atoi( p
);
266 if ( *ludp
->lud_host
== '\0' ) {
268 * no hostname and a port: invalid hostcode
269 * according to RFC 1738
271 ldap_free_urldesc(ludp
);
272 return (LDAP_URL_ERR_HOSTPORT
);
277 /* scan for '?' that marks end of dn and beginning of attributes */
279 if ( ludp
->lud_dn
!= NULL
&&
280 ( attrs
= strchr( ludp
->lud_dn
, '?' )) != NULL
) {
281 /* terminate dn; point to start of attrs. */
284 /* scan for '?' that marks end of attrs and begin. of scope */
285 if (( p
= strchr( attrs
, '?' )) != NULL
) {
287 * terminate attrs; point to start of scope and scan for
288 * '?' that marks end of scope and begin. of filter
293 if (( p
= strchr( scope
, '?' )) != NULL
) {
294 /* terminate scope; point to start of filter */
297 ludp
->lud_filter
= p
;
299 * scan for the '?' that marks the end
300 * of the filter and the start of any
303 if (( p
= strchr( ludp
->lud_filter
, '?' ))
305 *p
++ = '\0'; /* term. filter */
308 if ( *ludp
->lud_filter
== '\0' ) {
309 ludp
->lud_filter
= NULL
;
311 nsldapi_hex_unescape( ludp
->lud_filter
);
317 if ( strcasecmp( scope
, "one" ) == 0 ) {
318 ludp
->lud_scope
= LDAP_SCOPE_ONELEVEL
;
319 } else if ( strcasecmp( scope
, "base" ) == 0 ) {
320 ludp
->lud_scope
= LDAP_SCOPE_BASE
;
321 } else if ( strcasecmp( scope
, "sub" ) == 0 ) {
322 ludp
->lud_scope
= LDAP_SCOPE_SUBTREE
;
323 } else if ( *scope
!= '\0' ) {
324 ldap_free_urldesc( ludp
);
325 return( LDAP_URL_ERR_BADSCOPE
);
330 if ( ludp
->lud_dn
!= NULL
) {
331 nsldapi_hex_unescape( ludp
->lud_dn
);
335 * if attrs list was included, turn it into a null-terminated array
337 if ( attrs
!= NULL
&& *attrs
!= '\0' ) {
338 nsldapi_hex_unescape( attrs
);
339 for ( nattrs
= 1, p
= attrs
; *p
!= '\0'; ++p
) {
345 if (( ludp
->lud_attrs
= (char **)NSLDAPI_CALLOC( nattrs
+ 1,
346 sizeof( char * ))) == NULL
) {
347 ldap_free_urldesc( ludp
);
348 return( LDAP_URL_ERR_MEM
);
351 for ( i
= 0, p
= attrs
; i
< nattrs
; ++i
) {
352 ludp
->lud_attrs
[ i
] = p
;
353 if (( p
= strchr( p
, ',' )) != NULL
) {
356 nsldapi_hex_unescape( ludp
->lud_attrs
[ i
] );
360 /* if extensions list was included, check for critical ones */
361 if ( extensions
!= NULL
&& *extensions
!= '\0' ) {
362 /* Note: at present, we do not recognize ANY extensions */
364 for ( p
= extensions
; *p
!= '\0'; ++p
) {
366 if ( *p
== '!' ) { /* critical extension */
367 ldap_free_urldesc( ludp
);
368 /* this is what iplanet did *
369 return( LDAP_URL_UNRECOGNIZED_CRITICAL_EXTENSION );
370 * and this is what we do */
371 return( LDAP_URL_ERR_PARAM
);
374 } else if ( *p
== ',' ) {
389 ldap_free_urldesc( LDAPURLDesc
*ludp
)
391 if ( ludp
!= NULLLDAPURLDESC
) {
392 if ( ludp
->lud_string
!= NULL
) {
393 NSLDAPI_FREE( ludp
->lud_string
);
395 if ( ludp
->lud_attrs
!= NULL
) {
396 NSLDAPI_FREE( ludp
->lud_attrs
);
398 NSLDAPI_FREE( ludp
);
405 ldap_url_search( LDAP
*ld
, const char *url
, int attrsonly
)
413 if ( !NSLDAPI_VALID_LDAP_POINTER( ld
)) {
414 return( -1 ); /* punt */
417 if ( ldap_url_parse( url
, &ludp
) != 0 ) {
418 LDAP_SET_LDERRNO( ld
, LDAP_PARAM_ERROR
, NULL
, NULL
);
422 LDAP_MUTEX_LOCK( ld
, LDAP_MSGID_LOCK
);
423 msgid
= ++ld
->ld_msgid
;
424 LDAP_MUTEX_UNLOCK( ld
, LDAP_MSGID_LOCK
);
426 if ( nsldapi_build_search_req( ld
, ludp
->lud_dn
, ludp
->lud_scope
,
427 ludp
->lud_filter
, ludp
->lud_attrs
, attrsonly
, NULL
, NULL
,
428 -1, -1, msgid
, &ber
) != LDAP_SUCCESS
) {
434 if ( ludp
->lud_host
== NULL
) {
435 host
= ld
->ld_defhost
;
437 host
= ludp
->lud_host
;
440 if (( srv
= (LDAPServer
*)NSLDAPI_CALLOC( 1, sizeof( LDAPServer
)))
441 == NULL
|| ( host
!= NULL
&&
442 ( srv
->lsrv_host
= nsldapi_strdup( host
)) == NULL
)) {
446 LDAP_SET_LDERRNO( ld
, LDAP_NO_MEMORY
, NULL
, NULL
);
449 if ( ludp
->lud_port
!= 0 ) {
450 /* URL includes a port - use it */
451 srv
->lsrv_port
= ludp
->lud_port
;
452 } else if ( ludp
->lud_host
== NULL
) {
453 /* URL has no port or host - use port from ld */
454 srv
->lsrv_port
= ld
->ld_defport
;
455 } else if (( ludp
->lud_options
& LDAP_URL_OPT_SECURE
) == 0 ) {
456 /* ldap URL has a host but no port - use std. port */
457 srv
->lsrv_port
= LDAP_PORT
;
459 /* ldaps URL has a host but no port - use std. port */
460 srv
->lsrv_port
= LDAPS_PORT
;
464 if (( ludp
->lud_options
& LDAP_URL_OPT_SECURE
) != 0 ) {
465 srv
->lsrv_options
|= LDAP_SRV_OPT_SECURE
;
471 err
= nsldapi_send_server_request( ld
, ber
, msgid
, NULL
, srv
,
475 ldap_free_urldesc( ludp
);
482 ldap_url_search_st( LDAP
*ld
, const char *url
, int attrsonly
,
483 struct timeval
*timeout
, LDAPMessage
**res
)
488 * It is an error to pass in a zero'd timeval.
490 if ( timeout
!= NULL
&& timeout
->tv_sec
== 0 &&
491 timeout
->tv_usec
== 0 ) {
493 LDAP_SET_LDERRNO( ld
, LDAP_PARAM_ERROR
, NULL
, NULL
);
498 return( LDAP_PARAM_ERROR
);
501 if (( msgid
= ldap_url_search( ld
, url
, attrsonly
)) == -1 ) {
502 return( LDAP_GET_LDERRNO( ld
, NULL
, NULL
) );
505 if ( ldap_result( ld
, msgid
, 1, timeout
, res
) == -1 ) {
506 return( LDAP_GET_LDERRNO( ld
, NULL
, NULL
) );
509 if ( LDAP_GET_LDERRNO( ld
, NULL
, NULL
) == LDAP_TIMEOUT
) {
510 (void) ldap_abandon( ld
, msgid
);
511 LDAP_SET_LDERRNO( ld
, LDAP_TIMEOUT
, NULL
, NULL
);
512 return( LDAP_TIMEOUT
);
515 return( ldap_result2error( ld
, *res
, 0 ));
521 ldap_url_search_s( LDAP
*ld
, const char *url
, int attrsonly
, LDAPMessage
**res
)
525 if (( msgid
= ldap_url_search( ld
, url
, attrsonly
)) == -1 ) {
526 return( LDAP_GET_LDERRNO( ld
, NULL
, NULL
) );
529 if ( ldap_result( ld
, msgid
, 1, (struct timeval
*)NULL
, res
) == -1 ) {
530 return( LDAP_GET_LDERRNO( ld
, NULL
, NULL
) );
533 return( ldap_result2error( ld
, *res
, 0 ));
538 * Locate the LDAP URL associated with a DNS domain name.
540 * The supplied DNS domain name is converted into a distinguished
541 * name. The directory entry specified by that distinguished name
542 * is searched for a labeledURI attribute. If successful then the
543 * LDAP URL is returned. If unsuccessful then that entry's parent
544 * is searched and so on until the target distinguished name is
545 * reduced to only two nameparts.
547 * For example, if 'ny.eng.wiz.com' is the DNS domain then the
548 * following entries are searched until one succeeds:
549 * dc=ny,dc=eng,dc=wiz,dc=com
550 * dc=eng,dc=wiz,dc=com
553 * If dns_name is NULL then the environment variable LOCALDOMAIN is used.
554 * If attrs is not NULL then it is appended to the URL's attribute list.
555 * If scope is not NULL then it overrides the URL's scope.
556 * If filter is not NULL then it is merged with the URL's filter.
558 * If an error is encountered then zero is returned, otherwise a string
559 * URL is returned. The caller should free the returned string if it is
575 LDAPURLDesc
*urldesc
;
578 size_t attrs_len
= 0;
579 size_t scope_len
= 0;
580 size_t filter_len
= 0;
586 dns_name
= (char *)getenv("LOCALDOMAIN");
589 if ((ld
== NULL
) || ((dn
= ldap_dns_to_dn(dns_name
, &nameparts
)) ==
593 if ((url
= ldap_dn_to_url(ld
, dn
, nameparts
)) == NULL
) {
599 /* merge filter and/or scope and/or attributes with URL */
600 if (attrs
|| scope
|| filter
) {
603 attrs_len
= strlen(attrs
) + 2; /* for comma and NULL */
606 scope_len
= strlen(scope
) + 1; /* for NULL */
609 filter_len
= strlen(filter
) + 4;
610 /* for ampersand, parentheses and NULL */
612 if (ldap_is_ldap_url(url
)) {
614 if ((url2
= (char *)malloc(attrs_len
+ scope_len
+
615 filter_len
+ strlen(url
) + 1)) == NULL
) {
621 /* copy URL scheme, hostname, port number and DN */
622 while (*cp
&& (*cp
!= '?')) {
626 /* handle URL attributes */
628 if (*cp
== '?') { /* test first '?' */
629 *cp2
++ = *cp
++; /* copy first '?' */
631 if (*cp
== '?') { /* test second '?' */
633 /* insert supplied attributes */
644 /* copy URL attributes */
645 while (*cp
&& (*cp
!= '?')) {
649 /* append supplied attributes */
659 /* append supplied attributes */
670 /* handle URL scope */
672 if (*cp
== '?') { /* test second '?' */
673 *cp2
++ = *cp
++; /* copy second '?' */
675 if (*cp
== '?') { /* test third '?' */
677 /* insert supplied scope */
689 /* skip over URL scope */
690 while (*cp
&& (*cp
!= '?')) {
693 /* insert supplied scope */
700 while (*cp
&& (*cp
!= '?')) {
707 /* append supplied scope */
721 /* handle URL filter */
723 if (*cp
== '?') { /* test third '?' */
724 *cp2
++ = *cp
++; /* copy third '?' */
728 /* merge URL and supplied filters */
732 /* copy URL filter */
736 /* append supplied filter */
743 /* copy URL filter */
750 /* append supplied filter */
770 return (0); /* not an LDAP URL */
777 * Locate the LDAP URL associated with a distinguished name.
779 * The number of nameparts in the supplied distinguished name must be
780 * provided. The specified directory entry is searched for a labeledURI
781 * attribute. If successful then the LDAP URL is returned. If unsuccessful
782 * then that entry's parent is searched and so on until the target
783 * distinguished name is reduced to only two nameparts.
785 * For example, if 'l=ny,ou=eng,o=wiz,c=us' is the distinguished name
786 * then the following entries are searched until one succeeds:
787 * l=ny,ou=eng,o=wiz,c=us
791 * If an error is encountered then zero is returned, otherwise a string
792 * URL is returned. The caller should free the returned string if it is
805 char *attrs
[2] = {"labeledURI", 0};
806 LDAPMessage
*res
, *e
;
810 * Search for a URL in the named entry or its parent entry.
811 * Continue until only 2 nameparts remain.
813 while (dn
&& (nameparts
> 1) && (! url
)) {
815 /* search for the labeledURI attribute */
816 if (ldap_search_s(ld
, dn
, LDAP_SCOPE_BASE
,
817 "(objectClass=*)", attrs
, 0, &res
) == LDAP_SUCCESS
) {
819 /* locate the first entry returned */
820 if ((e
= ldap_first_entry(ld
, res
)) != NULL
) {
822 /* locate the labeledURI attribute */
824 ldap_get_values(ld
, e
, "labeledURI")) !=
827 /* copy the attribute value */
828 if ((url
= strdup((char *)vals
[0])) !=
830 ldap_value_free(vals
);
834 /* free the search results */
841 /* advance along the DN by one namepart */
842 if (next_dn
= strchr(dn
, ',')) {
853 #endif /* _SOLARIS_SDK */