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 int skip_url_prefix LDAP_P(( char **urlp, int *enclosedp ));
39 static void hex_unescape LDAP_P(( char *s ));
40 static int unhex( char c );
44 ldap_is_ldap_url( char *url )
48 return( url != NULL && skip_url_prefix( &url, &enclosed ));
53 skip_url_prefix( char **urlp, 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
59 if ( *urlp == NULL ) {
63 /* skip leading '<' (if any) */
64 if ( **urlp == '<' ) {
71 /* skip leading "URL:" (if any) */
72 if ( strlen( *urlp ) >= LDAP_URL_URLCOLON_LEN && strncasecmp(
73 *urlp, LDAP_URL_URLCOLON, LDAP_URL_URLCOLON_LEN ) == 0 ) {
74 *urlp += LDAP_URL_URLCOLON_LEN;
77 /* check for missing "ldap://" prefix */
78 if ( strlen( *urlp ) < LDAP_URL_PREFIX_LEN ||
79 strncasecmp( *urlp, LDAP_URL_PREFIX, LDAP_URL_PREFIX_LEN ) != 0 ) {
83 /* skip over "ldap://" prefix and return success */
84 *urlp += LDAP_URL_PREFIX_LEN;
91 ldap_url_parse( char *url, LDAPURLDesc **ludpp )
94 * Pick apart the pieces of an LDAP URL.
99 int enclosed, i, nattrs;
101 Debug( LDAP_DEBUG_TRACE, "ldap_url_parse(%s)\n", url, 0, 0 );
103 *ludpp = NULL; /* pessimistic */
105 if ( !skip_url_prefix( &url, &enclosed )) {
106 return( LDAP_URL_ERR_NOTLDAP );
109 /* allocate return struct */
110 if (( ludp = (LDAPURLDesc *)calloc( 1, sizeof( LDAPURLDesc )))
111 == NULLLDAPURLDESC ) {
112 return( LDAP_URL_ERR_MEM );
115 /* make working copy of the remainder of the URL */
116 if (( url = strdup( url )) == NULL ) {
117 ldap_free_urldesc( ludp );
118 return( LDAP_URL_ERR_MEM );
121 if ( enclosed && *((p = url + strlen( url ) - 1)) == '>' ) {
126 ludp->lud_scope = LDAP_SCOPE_BASE;
127 ludp->lud_filter = "(objectClass=*)";
129 /* lud_string is the only malloc'd string space we use */
130 ludp->lud_string = url;
132 /* scan forward for '/' that marks end of hostport and begin. of dn */
133 if (( ludp->lud_dn = strchr( url, '/' )) == NULL ) {
134 ldap_free_urldesc( ludp );
135 return( LDAP_URL_ERR_NODN );
138 /* terminate hostport; point to start of dn */
139 *ludp->lud_dn++ = '\0';
141 if (( p = strchr( url, ':' )) != NULL ) {
143 ludp->lud_port = atoi( p );
146 if ( *url == '\0' ) {
147 ludp->lud_host = NULL;
149 ludp->lud_host = url;
150 hex_unescape( ludp->lud_host );
153 /* scan for '?' that marks end of dn and beginning of attributes */
154 if (( attrs = strchr( ludp->lud_dn, '?' )) != NULL ) {
155 /* terminate dn; point to start of attrs. */
158 /* scan for '?' that marks end of attrs and begin. of scope */
159 if (( p = strchr( attrs, '?' )) != NULL ) {
161 * terminate attrs; point to start of scope and scan for
162 * '?' that marks end of scope and begin. of filter
166 if (( q = strchr( p, '?' )) != NULL ) {
167 /* terminate scope; point to start of filter */
170 ludp->lud_filter = q;
171 hex_unescape( ludp->lud_filter );
175 if ( strcasecmp( p, "one" ) == 0 ) {
176 ludp->lud_scope = LDAP_SCOPE_ONELEVEL;
177 } else if ( strcasecmp( p, "base" ) == 0 ) {
178 ludp->lud_scope = LDAP_SCOPE_BASE;
179 } else if ( strcasecmp( p, "sub" ) == 0 ) {
180 ludp->lud_scope = LDAP_SCOPE_SUBTREE;
181 } else if ( *p != '\0' ) {
182 ldap_free_urldesc( ludp );
183 return( LDAP_URL_ERR_BADSCOPE );
188 if ( *ludp->lud_dn == '\0' ) {
191 hex_unescape( ludp->lud_dn );
195 * if attrs list was included, turn it into a null-terminated array
197 if ( attrs != NULL && *attrs != '\0' ) {
198 for ( nattrs = 1, p = attrs; *p != '\0'; ++p ) {
204 if (( ludp->lud_attrs = (char **)calloc( nattrs + 1,
205 sizeof( char * ))) == NULL ) {
206 ldap_free_urldesc( ludp );
207 return( LDAP_URL_ERR_MEM );
210 for ( i = 0, p = attrs; i < nattrs; ++i ) {
211 ludp->lud_attrs[ i ] = p;
212 if (( p = strchr( p, ',' )) != NULL ) {
215 hex_unescape( ludp->lud_attrs[ i ] );
226 ldap_free_urldesc( LDAPURLDesc *ludp )
228 if ( ludp != NULLLDAPURLDESC ) {
229 if ( ludp->lud_string != NULL ) {
230 free( ludp->lud_string );
232 if ( ludp->lud_attrs != NULL ) {
233 free( ludp->lud_attrs );
242 ldap_url_search( LDAP *ld, char *url, int attrsonly )
247 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
248 LDAPServer *srv = NULL;
249 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
251 if ( ldap_url_parse( url, &ludp ) != 0 ) {
252 ld->ld_errno = LDAP_PARAM_ERROR;
256 if (( ber = ldap_build_search_req( ld, ludp->lud_dn, ludp->lud_scope,
257 ludp->lud_filter, ludp->lud_attrs, attrsonly )) == NULLBER ) {
263 if ( ludp->lud_host != NULL || ludp->lud_port != 0 ) {
264 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
265 if (( srv = (LDAPServer *)calloc( 1, sizeof( LDAPServer )))
266 == NULL || ( srv->lsrv_host = strdup( ludp->lud_host ==
267 NULL ? ld->ld_defhost : ludp->lud_host )) == NULL ) {
271 ld->ld_errno = LDAP_NO_MEMORY;
274 if ( ludp->lud_port == 0 ) {
275 srv->lsrv_port = openldap_ldap_global_options.ldo_defport;
277 srv->lsrv_port = ludp->lud_port;
280 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
281 ld->ld_errno = LDAP_LOCAL_ERROR;
283 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
289 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
290 err = ldap_send_server_request( ld, ber, ld->ld_msgid, NULL, srv,
292 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
293 err = ldap_send_initial_request( ld, LDAP_REQ_SEARCH,
295 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
298 ldap_free_urldesc( ludp );
305 ldap_url_search_st( LDAP *ld, char *url, int attrsonly,
306 struct timeval *timeout, LDAPMessage **res )
310 if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
311 return( ld->ld_errno );
314 if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 ) {
315 return( ld->ld_errno );
318 if ( ld->ld_errno == LDAP_TIMEOUT ) {
319 (void) ldap_abandon( ld, msgid );
320 ld->ld_errno = LDAP_TIMEOUT;
321 return( ld->ld_errno );
324 return( ldap_result2error( ld, *res, 0 ));
329 ldap_url_search_s( LDAP *ld, char *url, int attrsonly, LDAPMessage **res )
333 if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
334 return( ld->ld_errno );
337 if ( ldap_result( ld, msgid, 1, (struct timeval *)NULL, res ) == -1 ) {
338 return( ld->ld_errno );
341 return( ldap_result2error( ld, *res, 0 ));
346 hex_unescape( char *s )
349 * Remove URL hex escapes from s... done in place. The basic concept for
350 * this routine is borrowed from the WWW library HTUnEscape() routine.
354 for ( p = s; *s != '\0'; ++s ) {
356 if ( *++s != '\0' ) {
357 *p = unhex( *s ) << 4;
359 if ( *++s != '\0' ) {
374 return( c >= '0' && c <= '9' ? c - '0'
375 : c >= 'A' && c <= 'F' ? c - 'A' + 10