2 * Copyright (c) 1996 Regents of the University of Michigan.
5 * LIBLDAP url.c -- LDAP URL related routines
7 * LDAP URLs look like this:
8 * l d a p : / / hostport / dn [ ? attributes [ ? scope [ ? filter ] ] ]
11 * attributes is a comma separated list
12 * scope is one of these three strings: base one sub (default=base)
13 * filter is an string-represented filter as in RFC 1558
15 * e.g., ldap://ldap.itd.umich.edu/c=US?o,description?one?o=umich
17 * We also tolerate URLs that look like: <ldapurl> and <URL:ldapurl>
21 static char copyright[] = "@(#) Copyright (c) 1996 Regents of the University of Michigan.\nAll rights reserved.\n";
33 #if defined( DOS ) || defined( _WIN32 )
37 #endif /* DOS || _WIN32 */
39 #if !defined(MACOS) && !defined(DOS) && !defined( _WIN32 )
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #endif /* !MACOS && !DOS && !_WIN32 */
51 static int skip_url_prefix( char **urlp, int *enclosedp );
52 static void hex_unescape( char *s );
53 static int unhex( char c );
54 #else /* NEEDPROTOS */
55 static int skip_url_prefix();
56 static void hex_unescape();
58 #endif /* NEEDPROTOS */
62 ldap_is_ldap_url( char *url )
66 return( url != NULL && skip_url_prefix( &url, &enclosed ));
71 skip_url_prefix( char **urlp, int *enclosedp )
74 * return non-zero if this looks like a LDAP URL; zero if not
75 * if non-zero returned, *urlp will be moved past "ldap://" part of URL
77 if ( *urlp == NULL ) {
81 /* skip leading '<' (if any) */
82 if ( **urlp == '<' ) {
89 /* skip leading "URL:" (if any) */
90 if ( strlen( *urlp ) >= LDAP_URL_URLCOLON_LEN && strncasecmp(
91 *urlp, LDAP_URL_URLCOLON, LDAP_URL_URLCOLON_LEN ) == 0 ) {
92 *urlp += LDAP_URL_URLCOLON_LEN;
95 /* check for missing "ldap://" prefix */
96 if ( strlen( *urlp ) < LDAP_URL_PREFIX_LEN ||
97 strncasecmp( *urlp, LDAP_URL_PREFIX, LDAP_URL_PREFIX_LEN ) != 0 ) {
101 /* skip over "ldap://" prefix and return success */
102 *urlp += LDAP_URL_PREFIX_LEN;
109 ldap_url_parse( char *url, LDAPURLDesc **ludpp )
112 * Pick apart the pieces of an LDAP URL.
117 int enclosed, i, nattrs;
119 Debug( LDAP_DEBUG_TRACE, "ldap_url_parse(%s)\n", url, 0, 0 );
121 *ludpp = NULL; /* pessimistic */
123 if ( !skip_url_prefix( &url, &enclosed )) {
124 return( LDAP_URL_ERR_NOTLDAP );
127 /* allocate return struct */
128 if (( ludp = (LDAPURLDesc *)calloc( 1, sizeof( LDAPURLDesc )))
129 == NULLLDAPURLDESC ) {
130 return( LDAP_URL_ERR_MEM );
133 /* make working copy of the remainder of the URL */
134 if (( url = strdup( url )) == NULL ) {
135 ldap_free_urldesc( ludp );
136 return( LDAP_URL_ERR_MEM );
139 if ( enclosed && *((p = url + strlen( url ) - 1)) == '>' ) {
144 ludp->lud_scope = LDAP_SCOPE_BASE;
145 ludp->lud_filter = "(objectClass=*)";
147 /* lud_string is the only malloc'd string space we use */
148 ludp->lud_string = url;
150 /* scan forward for '/' that marks end of hostport and begin. of dn */
151 if (( ludp->lud_dn = strchr( url, '/' )) == NULL ) {
152 ldap_free_urldesc( ludp );
153 return( LDAP_URL_ERR_NODN );
156 /* terminate hostport; point to start of dn */
157 *ludp->lud_dn++ = '\0';
159 if (( p = strchr( url, ':' )) != NULL ) {
161 ludp->lud_port = atoi( p );
164 if ( *url == '\0' ) {
165 ludp->lud_host = NULL;
167 ludp->lud_host = url;
168 hex_unescape( ludp->lud_host );
171 /* scan for '?' that marks end of dn and beginning of attributes */
172 if (( attrs = strchr( ludp->lud_dn, '?' )) != NULL ) {
173 /* terminate dn; point to start of attrs. */
176 /* scan for '?' that marks end of attrs and begin. of scope */
177 if (( p = strchr( attrs, '?' )) != NULL ) {
179 * terminate attrs; point to start of scope and scan for
180 * '?' that marks end of scope and begin. of filter
184 if (( q = strchr( p, '?' )) != NULL ) {
185 /* terminate scope; point to start of filter */
188 ludp->lud_filter = q;
189 hex_unescape( ludp->lud_filter );
193 if ( strcasecmp( p, "one" ) == 0 ) {
194 ludp->lud_scope = LDAP_SCOPE_ONELEVEL;
195 } else if ( strcasecmp( p, "base" ) == 0 ) {
196 ludp->lud_scope = LDAP_SCOPE_BASE;
197 } else if ( strcasecmp( p, "sub" ) == 0 ) {
198 ludp->lud_scope = LDAP_SCOPE_SUBTREE;
199 } else if ( *p != '\0' ) {
200 ldap_free_urldesc( ludp );
201 return( LDAP_URL_ERR_BADSCOPE );
206 if ( *ludp->lud_dn == '\0' ) {
209 hex_unescape( ludp->lud_dn );
213 * if attrs list was included, turn it into a null-terminated array
215 if ( attrs != NULL && *attrs != '\0' ) {
216 for ( nattrs = 1, p = attrs; *p != '\0'; ++p ) {
222 if (( ludp->lud_attrs = (char **)calloc( nattrs + 1,
223 sizeof( char * ))) == NULL ) {
224 ldap_free_urldesc( ludp );
225 return( LDAP_URL_ERR_MEM );
228 for ( i = 0, p = attrs; i < nattrs; ++i ) {
229 ludp->lud_attrs[ i ] = p;
230 if (( p = strchr( p, ',' )) != NULL ) {
233 hex_unescape( ludp->lud_attrs[ i ] );
244 ldap_free_urldesc( LDAPURLDesc *ludp )
246 if ( ludp != NULLLDAPURLDESC ) {
247 if ( ludp->lud_string != NULL ) {
248 free( ludp->lud_string );
250 if ( ludp->lud_attrs != NULL ) {
251 free( ludp->lud_attrs );
260 ldap_url_search( LDAP *ld, char *url, int attrsonly )
265 #ifdef LDAP_REFERRALS
266 LDAPServer *srv = NULL;
267 #endif /* LDAP_REFERRALS */
269 if ( ldap_url_parse( url, &ludp ) != 0 ) {
270 ld->ld_errno = LDAP_PARAM_ERROR;
274 if (( ber = ldap_build_search_req( ld, ludp->lud_dn, ludp->lud_scope,
275 ludp->lud_filter, ludp->lud_attrs, attrsonly )) == NULLBER ) {
281 if ( ludp->lud_host != NULL || ludp->lud_port != 0 ) {
282 #ifdef LDAP_REFERRALS
283 if (( srv = (LDAPServer *)calloc( 1, sizeof( LDAPServer )))
284 == NULL || ( srv->lsrv_host = strdup( ludp->lud_host ==
285 NULL ? ld->ld_defhost : ludp->lud_host )) == NULL ) {
289 ld->ld_errno = LDAP_NO_MEMORY;
292 if ( ludp->lud_port == 0 ) {
293 srv->lsrv_port = LDAP_PORT;
295 srv->lsrv_port = ludp->lud_port;
298 #else /* LDAP_REFERRALS */
299 ld->ld_errno = LDAP_LOCAL_ERROR;
301 #endif /* LDAP_REFERRALS */
307 #ifdef LDAP_REFERRALS
308 err = send_server_request( ld, ber, ld->ld_msgid, NULL, srv,
310 #else /* LDAP_REFERRALS */
311 err = send_initial_request( ld, LDAP_REQ_SEARCH,
313 #endif /* LDAP_REFERRALS */
316 ldap_free_urldesc( ludp );
323 ldap_url_search_st( LDAP *ld, char *url, int attrsonly,
324 struct timeval *timeout, LDAPMessage **res )
328 if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
329 return( ld->ld_errno );
332 if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 ) {
333 return( ld->ld_errno );
336 if ( ld->ld_errno == LDAP_TIMEOUT ) {
337 (void) ldap_abandon( ld, msgid );
338 ld->ld_errno = LDAP_TIMEOUT;
339 return( ld->ld_errno );
342 return( ldap_result2error( ld, *res, 0 ));
347 ldap_url_search_s( LDAP *ld, char *url, int attrsonly, LDAPMessage **res )
351 if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
352 return( ld->ld_errno );
355 if ( ldap_result( ld, msgid, 1, (struct timeval *)NULL, res ) == -1 ) {
356 return( ld->ld_errno );
359 return( ldap_result2error( ld, *res, 0 ));
364 hex_unescape( char *s )
367 * Remove URL hex escapes from s... done in place. The basic concept for
368 * this routine is borrowed from the WWW library HTUnEscape() routine.
372 for ( p = s; *s != '\0'; ++s ) {
374 if ( *++s != '\0' ) {
375 *p = unhex( *s ) << 4;
377 if ( *++s != '\0' ) {
392 return( c >= '0' && c <= '9' ? c - '0'
393 : c >= 'A' && c <= 'F' ? c - 'A' + 10