2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 * Copyright (c) 1996 Regents of the University of Michigan.
9 * LIBLDAP url.c -- LDAP URL related routines
11 * LDAP URLs look like this:
12 * l d a p : / / hostport / dn [ ? attributes [ ? scope [ ? filter ] ] ]
15 * attributes is a comma separated list
16 * scope is one of these three strings: base one sub (default=base)
17 * filter is an string-represented filter as in RFC 1558
19 * e.g., ldap://ldap.itd.umich.edu/c=US?o,description?one?o=umich
21 * We also tolerate URLs that look like: <ldapurl> and <URL:ldapurl>
30 #include <ac/socket.h>
31 #include <ac/string.h>
38 static const char* skip_url_prefix LDAP_P(( const char *url, int *enclosedp ));
39 static void hex_unescape LDAP_P(( char *s ));
40 static int unhex( char c );
44 ldap_is_ldap_url( LDAP_CONST char *url )
48 return( url != NULL && skip_url_prefix( url, &enclosed ) != NULL );
53 skip_url_prefix( const char *url, int *enclosedp )
56 * return non-zero if this looks like a LDAP URL; zero if not
57 * if non-zero returned, *urlp will be moved past "ldap://" part of URL
67 /* skip leading '<' (if any) */
75 /* skip leading "URL:" (if any) */
76 if ( strlen( p ) >= LDAP_URL_URLCOLON_LEN
77 && strncasecmp( p, LDAP_URL_URLCOLON, LDAP_URL_URLCOLON_LEN ) == 0 )
79 p += LDAP_URL_URLCOLON_LEN;
82 /* check for missing "ldap://" prefix */
83 if ( strlen( p ) < LDAP_URL_PREFIX_LEN ||
84 strncasecmp( p, LDAP_URL_PREFIX, LDAP_URL_PREFIX_LEN ) != 0 ) {
88 /* skip over "ldap://" prefix and return success */
89 p += LDAP_URL_PREFIX_LEN;
96 ldap_url_parse( LDAP_CONST char *url_in, LDAPURLDesc **ludpp )
99 * Pick apart the pieces of an LDAP URL.
104 int enclosed, i, nattrs;
108 Debug( LDAP_DEBUG_TRACE, "ldap_url_parse(%s)\n", url_in, 0, 0 );
110 *ludpp = NULL; /* pessimistic */
112 url_tmp = skip_url_prefix( url_in, &enclosed );
114 if ( url_tmp == NULL ) {
115 return( LDAP_URL_ERR_NOTLDAP );
118 /* make working copy of the remainder of the URL */
119 if (( url = strdup( url_tmp )) == NULL ) {
120 return( LDAP_URL_ERR_MEM );
123 /* allocate return struct */
124 if (( ludp = (LDAPURLDesc *)calloc( 1, sizeof( LDAPURLDesc )))
128 return( LDAP_URL_ERR_MEM );
132 if ( enclosed && *((p = url + strlen( url ) - 1)) == '>' ) {
137 ludp->lud_scope = LDAP_SCOPE_BASE;
138 ludp->lud_filter = "(objectClass=*)";
140 /* lud_string is the only malloc'd string space we use */
141 ludp->lud_string = url;
143 /* scan forward for '/' that marks end of hostport and begin. of dn */
144 if (( ludp->lud_dn = strchr( url, '/' )) == NULL ) {
145 ldap_free_urldesc( ludp );
146 return( LDAP_URL_ERR_NODN );
149 /* terminate hostport; point to start of dn */
150 *ludp->lud_dn++ = '\0';
152 if (( p = strchr( url, ':' )) != NULL ) {
154 ludp->lud_port = atoi( p );
157 if ( *url == '\0' ) {
158 ludp->lud_host = NULL;
160 ludp->lud_host = url;
161 hex_unescape( ludp->lud_host );
164 /* scan for '?' that marks end of dn and beginning of attributes */
165 if (( attrs = strchr( ludp->lud_dn, '?' )) != NULL ) {
166 /* terminate dn; point to start of attrs. */
169 /* scan for '?' that marks end of attrs and begin. of scope */
170 if (( p = strchr( attrs, '?' )) != NULL ) {
172 * terminate attrs; point to start of scope and scan for
173 * '?' that marks end of scope and begin. of filter
177 if (( q = strchr( p, '?' )) != NULL ) {
178 /* terminate scope; point to start of filter */
181 ludp->lud_filter = q;
182 hex_unescape( ludp->lud_filter );
186 if ( strcasecmp( p, "one" ) == 0 ) {
187 ludp->lud_scope = LDAP_SCOPE_ONELEVEL;
188 } else if ( strcasecmp( p, "base" ) == 0 ) {
189 ludp->lud_scope = LDAP_SCOPE_BASE;
190 } else if ( strcasecmp( p, "sub" ) == 0 ) {
191 ludp->lud_scope = LDAP_SCOPE_SUBTREE;
192 } else if ( *p != '\0' ) {
193 ldap_free_urldesc( ludp );
194 return( LDAP_URL_ERR_BADSCOPE );
199 if ( *ludp->lud_dn == '\0' ) {
202 hex_unescape( ludp->lud_dn );
206 * if attrs list was included, turn it into a null-terminated array
208 if ( attrs != NULL && *attrs != '\0' ) {
209 for ( nattrs = 1, p = attrs; *p != '\0'; ++p ) {
215 if (( ludp->lud_attrs = (char **)calloc( nattrs + 1,
216 sizeof( char * ))) == NULL ) {
217 ldap_free_urldesc( ludp );
218 return( LDAP_URL_ERR_MEM );
221 for ( i = 0, p = attrs; i < nattrs; ++i ) {
222 ludp->lud_attrs[ i ] = p;
223 if (( p = strchr( p, ',' )) != NULL ) {
226 hex_unescape( ludp->lud_attrs[ i ] );
237 ldap_free_urldesc( LDAPURLDesc *ludp )
239 if ( ludp != NULLLDAPURLDESC ) {
240 if ( ludp->lud_string != NULL ) {
241 free( ludp->lud_string );
243 if ( ludp->lud_attrs != NULL ) {
244 free( ludp->lud_attrs );
253 ldap_url_search( LDAP *ld, LDAP_CONST char *url, int attrsonly )
258 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
259 LDAPServer *srv = NULL;
260 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
262 if ( ldap_url_parse( url, &ludp ) != 0 ) {
263 ld->ld_errno = LDAP_PARAM_ERROR;
267 ber = ldap_build_search_req( ld, ludp->lud_dn, ludp->lud_scope,
268 ludp->lud_filter, ludp->lud_attrs, attrsonly, NULL, NULL,
271 if ( ber == NULLBER ) {
277 if ( ludp->lud_host != NULL || ludp->lud_port != 0 ) {
278 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
279 if (( srv = (LDAPServer *)calloc( 1, sizeof( LDAPServer )))
280 == NULL || ( srv->lsrv_host = strdup( ludp->lud_host ==
281 NULL ? ld->ld_defhost : ludp->lud_host )) == NULL ) {
285 ld->ld_errno = LDAP_NO_MEMORY;
288 if ( ludp->lud_port == 0 ) {
289 srv->lsrv_port = openldap_ldap_global_options.ldo_defport;
291 srv->lsrv_port = ludp->lud_port;
294 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
295 ld->ld_errno = LDAP_LOCAL_ERROR;
297 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
303 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
304 err = ldap_send_server_request( ld, ber, ld->ld_msgid, NULL, srv,
306 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
307 err = ldap_send_initial_request( ld, LDAP_REQ_SEARCH,
309 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
312 ldap_free_urldesc( ludp );
319 ldap_url_search_st( LDAP *ld, LDAP_CONST char *url, int attrsonly,
320 struct timeval *timeout, LDAPMessage **res )
324 if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
325 return( ld->ld_errno );
328 if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 ) {
329 return( ld->ld_errno );
332 if ( ld->ld_errno == LDAP_TIMEOUT ) {
333 (void) ldap_abandon( ld, msgid );
334 ld->ld_errno = LDAP_TIMEOUT;
335 return( ld->ld_errno );
338 return( ldap_result2error( ld, *res, 0 ));
344 LDAP *ld, LDAP_CONST char *url, int attrsonly, LDAPMessage **res )
348 if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
349 return( ld->ld_errno );
352 if ( ldap_result( ld, msgid, 1, (struct timeval *)NULL, res ) == -1 ) {
353 return( ld->ld_errno );
356 return( ldap_result2error( ld, *res, 0 ));
361 hex_unescape( char *s )
364 * Remove URL hex escapes from s... done in place. The basic concept for
365 * this routine is borrowed from the WWW library HTUnEscape() routine.
369 for ( p = s; *s != '\0'; ++s ) {
371 if ( *++s != '\0' ) {
372 *p = unhex( *s ) << 4;
374 if ( *++s != '\0' ) {
389 return( c >= '0' && c <= '9' ? c - '0'
390 : c >= 'A' && c <= 'F' ? c - 'A' + 10