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>
37 static int skip_url_prefix LDAP_P(( char **urlp, int *enclosedp ));
38 static void hex_unescape LDAP_P(( char *s ));
39 static int unhex LDAP_P(( char c ));
43 ldap_is_ldap_url( char *url )
47 return( url != NULL && skip_url_prefix( &url, &enclosed ));
52 skip_url_prefix( char **urlp, int *enclosedp )
55 * return non-zero if this looks like a LDAP URL; zero if not
56 * if non-zero returned, *urlp will be moved past "ldap://" part of URL
58 if ( *urlp == NULL ) {
62 /* skip leading '<' (if any) */
63 if ( **urlp == '<' ) {
70 /* skip leading "URL:" (if any) */
71 if ( strlen( *urlp ) >= LDAP_URL_URLCOLON_LEN && strncasecmp(
72 *urlp, LDAP_URL_URLCOLON, LDAP_URL_URLCOLON_LEN ) == 0 ) {
73 *urlp += LDAP_URL_URLCOLON_LEN;
76 /* check for missing "ldap://" prefix */
77 if ( strlen( *urlp ) < LDAP_URL_PREFIX_LEN ||
78 strncasecmp( *urlp, LDAP_URL_PREFIX, LDAP_URL_PREFIX_LEN ) != 0 ) {
82 /* skip over "ldap://" prefix and return success */
83 *urlp += LDAP_URL_PREFIX_LEN;
90 ldap_url_parse( char *url, LDAPURLDesc **ludpp )
93 * Pick apart the pieces of an LDAP URL.
98 int enclosed, i, nattrs;
100 Debug( LDAP_DEBUG_TRACE, "ldap_url_parse(%s)\n", url, 0, 0 );
102 *ludpp = NULL; /* pessimistic */
104 if ( !skip_url_prefix( &url, &enclosed )) {
105 return( LDAP_URL_ERR_NOTLDAP );
108 /* allocate return struct */
109 if (( ludp = (LDAPURLDesc *)calloc( 1, sizeof( LDAPURLDesc )))
110 == NULLLDAPURLDESC ) {
111 return( LDAP_URL_ERR_MEM );
114 /* make working copy of the remainder of the URL */
115 if (( url = strdup( url )) == NULL ) {
116 ldap_free_urldesc( ludp );
117 return( LDAP_URL_ERR_MEM );
120 if ( enclosed && *((p = url + strlen( url ) - 1)) == '>' ) {
125 ludp->lud_scope = LDAP_SCOPE_BASE;
126 ludp->lud_filter = "(objectClass=*)";
128 /* lud_string is the only malloc'd string space we use */
129 ludp->lud_string = url;
131 /* scan forward for '/' that marks end of hostport and begin. of dn */
132 if (( ludp->lud_dn = strchr( url, '/' )) == NULL ) {
133 ldap_free_urldesc( ludp );
134 return( LDAP_URL_ERR_NODN );
137 /* terminate hostport; point to start of dn */
138 *ludp->lud_dn++ = '\0';
140 if (( p = strchr( url, ':' )) != NULL ) {
142 ludp->lud_port = atoi( p );
145 if ( *url == '\0' ) {
146 ludp->lud_host = NULL;
148 ludp->lud_host = url;
149 hex_unescape( ludp->lud_host );
152 /* scan for '?' that marks end of dn and beginning of attributes */
153 if (( attrs = strchr( ludp->lud_dn, '?' )) != NULL ) {
154 /* terminate dn; point to start of attrs. */
157 /* scan for '?' that marks end of attrs and begin. of scope */
158 if (( p = strchr( attrs, '?' )) != NULL ) {
160 * terminate attrs; point to start of scope and scan for
161 * '?' that marks end of scope and begin. of filter
165 if (( q = strchr( p, '?' )) != NULL ) {
166 /* terminate scope; point to start of filter */
169 ludp->lud_filter = q;
170 hex_unescape( ludp->lud_filter );
174 if ( strcasecmp( p, "one" ) == 0 ) {
175 ludp->lud_scope = LDAP_SCOPE_ONELEVEL;
176 } else if ( strcasecmp( p, "base" ) == 0 ) {
177 ludp->lud_scope = LDAP_SCOPE_BASE;
178 } else if ( strcasecmp( p, "sub" ) == 0 ) {
179 ludp->lud_scope = LDAP_SCOPE_SUBTREE;
180 } else if ( *p != '\0' ) {
181 ldap_free_urldesc( ludp );
182 return( LDAP_URL_ERR_BADSCOPE );
187 if ( *ludp->lud_dn == '\0' ) {
190 hex_unescape( ludp->lud_dn );
194 * if attrs list was included, turn it into a null-terminated array
196 if ( attrs != NULL && *attrs != '\0' ) {
197 for ( nattrs = 1, p = attrs; *p != '\0'; ++p ) {
203 if (( ludp->lud_attrs = (char **)calloc( nattrs + 1,
204 sizeof( char * ))) == NULL ) {
205 ldap_free_urldesc( ludp );
206 return( LDAP_URL_ERR_MEM );
209 for ( i = 0, p = attrs; i < nattrs; ++i ) {
210 ludp->lud_attrs[ i ] = p;
211 if (( p = strchr( p, ',' )) != NULL ) {
214 hex_unescape( ludp->lud_attrs[ i ] );
225 ldap_free_urldesc( LDAPURLDesc *ludp )
227 if ( ludp != NULLLDAPURLDESC ) {
228 if ( ludp->lud_string != NULL ) {
229 free( ludp->lud_string );
231 if ( ludp->lud_attrs != NULL ) {
232 free( ludp->lud_attrs );
241 ldap_url_search( LDAP *ld, char *url, int attrsonly )
246 #ifdef LDAP_REFERRALS
247 LDAPServer *srv = NULL;
248 #endif /* LDAP_REFERRALS */
250 if ( ldap_url_parse( url, &ludp ) != 0 ) {
251 ld->ld_errno = LDAP_PARAM_ERROR;
255 if (( ber = ldap_build_search_req( ld, ludp->lud_dn, ludp->lud_scope,
256 ludp->lud_filter, ludp->lud_attrs, attrsonly )) == NULLBER ) {
262 if ( ludp->lud_host != NULL || ludp->lud_port != 0 ) {
263 #ifdef LDAP_REFERRALS
264 if (( srv = (LDAPServer *)calloc( 1, sizeof( LDAPServer )))
265 == NULL || ( srv->lsrv_host = strdup( ludp->lud_host ==
266 NULL ? ld->ld_defhost : ludp->lud_host )) == NULL ) {
270 ld->ld_errno = LDAP_NO_MEMORY;
273 if ( ludp->lud_port == 0 ) {
274 srv->lsrv_port = LDAP_PORT;
276 srv->lsrv_port = ludp->lud_port;
279 #else /* LDAP_REFERRALS */
280 ld->ld_errno = LDAP_LOCAL_ERROR;
282 #endif /* LDAP_REFERRALS */
288 #ifdef LDAP_REFERRALS
289 err = ldap_send_server_request( ld, ber, ld->ld_msgid, NULL, srv,
291 #else /* LDAP_REFERRALS */
292 err = ldap_send_initial_request( ld, LDAP_REQ_SEARCH,
294 #endif /* LDAP_REFERRALS */
297 ldap_free_urldesc( ludp );
304 ldap_url_search_st( LDAP *ld, char *url, int attrsonly,
305 struct timeval *timeout, LDAPMessage **res )
309 if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
310 return( ld->ld_errno );
313 if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 ) {
314 return( ld->ld_errno );
317 if ( ld->ld_errno == LDAP_TIMEOUT ) {
318 (void) ldap_abandon( ld, msgid );
319 ld->ld_errno = LDAP_TIMEOUT;
320 return( ld->ld_errno );
323 return( ldap_result2error( ld, *res, 0 ));
328 ldap_url_search_s( LDAP *ld, char *url, int attrsonly, LDAPMessage **res )
332 if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
333 return( ld->ld_errno );
336 if ( ldap_result( ld, msgid, 1, (struct timeval *)NULL, res ) == -1 ) {
337 return( ld->ld_errno );
340 return( ldap_result2error( ld, *res, 0 ));
345 hex_unescape( char *s )
348 * Remove URL hex escapes from s... done in place. The basic concept for
349 * this routine is borrowed from the WWW library HTUnEscape() routine.
353 for ( p = s; *s != '\0'; ++s ) {
355 if ( *++s != '\0' ) {
356 *p = unhex( *s ) << 4;
358 if ( *++s != '\0' ) {
373 return( c >= '0' && c <= '9' ? c - '0'
374 : c >= 'A' && c <= 'F' ? c - 'A' + 10