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>
23 static char copyright[] = "@(#) Copyright (c) 1996 Regents of the University of Michigan.\nAll rights reserved.\n";
30 #include <ac/socket.h>
31 #include <ac/string.h>
33 extern char *strdup (const char *);
39 static int skip_url_prefix LDAP_P(( char **urlp, int *enclosedp ));
40 static void hex_unescape LDAP_P(( char *s ));
41 static int unhex( char c );
45 ldap_is_ldap_url( char *url )
49 return( url != NULL && skip_url_prefix( &url, &enclosed ));
54 skip_url_prefix( char **urlp, int *enclosedp )
57 * return non-zero if this looks like a LDAP URL; zero if not
58 * if non-zero returned, *urlp will be moved past "ldap://" part of URL
60 if ( *urlp == NULL ) {
64 /* skip leading '<' (if any) */
65 if ( **urlp == '<' ) {
72 /* skip leading "URL:" (if any) */
73 if ( strlen( *urlp ) >= LDAP_URL_URLCOLON_LEN && strncasecmp(
74 *urlp, LDAP_URL_URLCOLON, LDAP_URL_URLCOLON_LEN ) == 0 ) {
75 *urlp += LDAP_URL_URLCOLON_LEN;
78 /* check for missing "ldap://" prefix */
79 if ( strlen( *urlp ) < LDAP_URL_PREFIX_LEN ||
80 strncasecmp( *urlp, LDAP_URL_PREFIX, LDAP_URL_PREFIX_LEN ) != 0 ) {
84 /* skip over "ldap://" prefix and return success */
85 *urlp += LDAP_URL_PREFIX_LEN;
92 ldap_url_parse( char *url, LDAPURLDesc **ludpp )
95 * Pick apart the pieces of an LDAP URL.
100 int enclosed, i, nattrs;
102 Debug( LDAP_DEBUG_TRACE, "ldap_url_parse(%s)\n", url, 0, 0 );
104 *ludpp = NULL; /* pessimistic */
106 if ( !skip_url_prefix( &url, &enclosed )) {
107 return( LDAP_URL_ERR_NOTLDAP );
110 /* allocate return struct */
111 if (( ludp = (LDAPURLDesc *)calloc( 1, sizeof( LDAPURLDesc )))
112 == NULLLDAPURLDESC ) {
113 return( LDAP_URL_ERR_MEM );
116 /* make working copy of the remainder of the URL */
117 if (( url = strdup( url )) == NULL ) {
118 ldap_free_urldesc( ludp );
119 return( LDAP_URL_ERR_MEM );
122 if ( enclosed && *((p = url + strlen( url ) - 1)) == '>' ) {
127 ludp->lud_scope = LDAP_SCOPE_BASE;
128 ludp->lud_filter = "(objectClass=*)";
130 /* lud_string is the only malloc'd string space we use */
131 ludp->lud_string = url;
133 /* scan forward for '/' that marks end of hostport and begin. of dn */
134 if (( ludp->lud_dn = strchr( url, '/' )) == NULL ) {
135 ldap_free_urldesc( ludp );
136 return( LDAP_URL_ERR_NODN );
139 /* terminate hostport; point to start of dn */
140 *ludp->lud_dn++ = '\0';
142 if (( p = strchr( url, ':' )) != NULL ) {
144 ludp->lud_port = atoi( p );
147 if ( *url == '\0' ) {
148 ludp->lud_host = NULL;
150 ludp->lud_host = url;
151 hex_unescape( ludp->lud_host );
154 /* scan for '?' that marks end of dn and beginning of attributes */
155 if (( attrs = strchr( ludp->lud_dn, '?' )) != NULL ) {
156 /* terminate dn; point to start of attrs. */
159 /* scan for '?' that marks end of attrs and begin. of scope */
160 if (( p = strchr( attrs, '?' )) != NULL ) {
162 * terminate attrs; point to start of scope and scan for
163 * '?' that marks end of scope and begin. of filter
167 if (( q = strchr( p, '?' )) != NULL ) {
168 /* terminate scope; point to start of filter */
171 ludp->lud_filter = q;
172 hex_unescape( ludp->lud_filter );
176 if ( strcasecmp( p, "one" ) == 0 ) {
177 ludp->lud_scope = LDAP_SCOPE_ONELEVEL;
178 } else if ( strcasecmp( p, "base" ) == 0 ) {
179 ludp->lud_scope = LDAP_SCOPE_BASE;
180 } else if ( strcasecmp( p, "sub" ) == 0 ) {
181 ludp->lud_scope = LDAP_SCOPE_SUBTREE;
182 } else if ( *p != '\0' ) {
183 ldap_free_urldesc( ludp );
184 return( LDAP_URL_ERR_BADSCOPE );
189 if ( *ludp->lud_dn == '\0' ) {
192 hex_unescape( ludp->lud_dn );
196 * if attrs list was included, turn it into a null-terminated array
198 if ( attrs != NULL && *attrs != '\0' ) {
199 for ( nattrs = 1, p = attrs; *p != '\0'; ++p ) {
205 if (( ludp->lud_attrs = (char **)calloc( nattrs + 1,
206 sizeof( char * ))) == NULL ) {
207 ldap_free_urldesc( ludp );
208 return( LDAP_URL_ERR_MEM );
211 for ( i = 0, p = attrs; i < nattrs; ++i ) {
212 ludp->lud_attrs[ i ] = p;
213 if (( p = strchr( p, ',' )) != NULL ) {
216 hex_unescape( ludp->lud_attrs[ i ] );
227 ldap_free_urldesc( LDAPURLDesc *ludp )
229 if ( ludp != NULLLDAPURLDESC ) {
230 if ( ludp->lud_string != NULL ) {
231 free( ludp->lud_string );
233 if ( ludp->lud_attrs != NULL ) {
234 free( ludp->lud_attrs );
243 ldap_url_search( LDAP *ld, char *url, int attrsonly )
248 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
249 LDAPServer *srv = NULL;
250 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
252 if ( ldap_url_parse( url, &ludp ) != 0 ) {
253 ld->ld_errno = LDAP_PARAM_ERROR;
257 if (( ber = ldap_build_search_req( ld, ludp->lud_dn, ludp->lud_scope,
258 ludp->lud_filter, ludp->lud_attrs, attrsonly )) == NULLBER ) {
264 if ( ludp->lud_host != NULL || ludp->lud_port != 0 ) {
265 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
266 if (( srv = (LDAPServer *)calloc( 1, sizeof( LDAPServer )))
267 == NULL || ( srv->lsrv_host = strdup( ludp->lud_host ==
268 NULL ? ld->ld_defhost : ludp->lud_host )) == NULL ) {
272 ld->ld_errno = LDAP_NO_MEMORY;
275 if ( ludp->lud_port == 0 ) {
276 srv->lsrv_port = LDAP_PORT;
278 srv->lsrv_port = ludp->lud_port;
281 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
282 ld->ld_errno = LDAP_LOCAL_ERROR;
284 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
290 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
291 err = ldap_send_server_request( ld, ber, ld->ld_msgid, NULL, srv,
293 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
294 err = ldap_send_initial_request( ld, LDAP_REQ_SEARCH,
296 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
299 ldap_free_urldesc( ludp );
306 ldap_url_search_st( LDAP *ld, char *url, int attrsonly,
307 struct timeval *timeout, LDAPMessage **res )
311 if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
312 return( ld->ld_errno );
315 if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 ) {
316 return( ld->ld_errno );
319 if ( ld->ld_errno == LDAP_TIMEOUT ) {
320 (void) ldap_abandon( ld, msgid );
321 ld->ld_errno = LDAP_TIMEOUT;
322 return( ld->ld_errno );
325 return( ldap_result2error( ld, *res, 0 ));
330 ldap_url_search_s( LDAP *ld, char *url, int attrsonly, LDAPMessage **res )
334 if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
335 return( ld->ld_errno );
338 if ( ldap_result( ld, msgid, 1, (struct timeval *)NULL, res ) == -1 ) {
339 return( ld->ld_errno );
342 return( ldap_result2error( ld, *res, 0 ));
347 hex_unescape( char *s )
350 * Remove URL hex escapes from s... done in place. The basic concept for
351 * this routine is borrowed from the WWW library HTUnEscape() routine.
355 for ( p = s; *s != '\0'; ++s ) {
357 if ( *++s != '\0' ) {
358 *p = unhex( *s ) << 4;
360 if ( *++s != '\0' ) {
375 return( c >= '0' && c <= '9' ? c - '0'
376 : c >= 'A' && c <= 'F' ? c - 'A' + 10