2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 * Copyright (c) 1990, 1994 Regents of the University of Michigan.
9 * cldap.c - synchronous, retrying interface to the cldap protocol
14 #ifdef LDAP_CONNECTIONLESS
20 #include <ac/socket.h>
21 #include <ac/string.h>
23 #include <ac/unistd.h>
27 #define DEF_CLDAP_TIMEOUT 3
28 #define DEF_CLDAP_TRIES 4
31 struct cldap_retinfo {
38 static int add_addr LDAP_P((
39 LDAP *ld, struct sockaddr *sap ));
40 static int cldap_result LDAP_P((
41 LDAP *ld, int msgid, LDAPMessage **res,
42 struct cldap_retinfo *crip, char *base ));
43 static int cldap_parsemsg LDAP_P((
44 LDAP *ld, int msgid, BerElement *ber,
45 LDAPMessage **res, char *base ));
48 * cldap_open - initialize and connect to an ldap server. A magic cookie to
49 * be used for future communication is returned on success, NULL on failure.
53 * ld = cldap_open( hostname, port );
57 cldap_open( char *host, int port )
60 unsigned long address;
61 struct sockaddr_in sock;
67 /* buffers for ldap_pvt_gethostbyname_a ... */
68 struct hostent he_buf;
72 #define DO_RETURN(x) if (ha_buf) free(ha_buf); return (x);
74 Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
76 if ( (s = socket( AF_INET, SOCK_DGRAM, 0 )) < 0 ) {
80 sock.sin_addr.s_addr = 0;
81 sock.sin_family = AF_INET;
83 if ( bind(s, (struct sockaddr *) &sock, sizeof(sock)) < 0) {
88 if (( ld = ldap_init( host, port )) == NULL ) {
92 if ( (ld->ld_sb.sb_fromaddr = (void *) calloc( 1,
93 sizeof( struct sockaddr ))) == NULL ) {
99 ld->ld_sb.sb_naddr = 0;
100 ld->ld_version = LDAP_VERSION;
102 sock.sin_family = AF_INET;
103 sock.sin_port = htons( port );
106 * 'host' may be a space-separated list.
108 if ( host != NULL ) {
109 for ( ; host != NULL; host = p ) {
110 if (( p = strchr( host, ' ' )) != NULL ) {
111 for (*p++ = '\0'; *p == ' '; p++) {
116 address = inet_addr( host );
117 /* This was just a test for -1 until OSF1 let inet_addr return
118 unsigned int, which is narrower than 'unsigned long address' */
119 if ( address == 0xffffffff || address == (unsigned long) -1 ) {
120 if ((ldap_pvt_gethostbyname_a( host, &he_buf, &ha_buf,
121 &hp,&local_h_errno)<0) ||
123 errno = EHOSTUNREACH;
127 for ( i = 0; hp->h_addr_list[ i ] != 0; ++i ) {
128 SAFEMEMCPY( (char *)&sock.sin_addr.s_addr,
129 (char *)hp->h_addr_list[ i ],
130 sizeof(sock.sin_addr.s_addr));
131 if ( add_addr( ld, (struct sockaddr *)&sock ) < 0 ) {
132 ldap_ld_free( ld, 1 );
138 sock.sin_addr.s_addr = address;
139 if ( add_addr( ld, (struct sockaddr *)&sock ) < 0 ) {
140 ldap_ld_free( ld, 1 );
145 if ( ld->ld_host == NULL ) {
146 ld->ld_host = strdup( host );
150 address = INADDR_LOOPBACK;
151 sock.sin_addr.s_addr = htonl( address );
152 if ( add_addr( ld, (struct sockaddr *)&sock ) < 0 ) {
153 ldap_ld_free( ld, 1 );
158 if ( ld->ld_sb.sb_addrs == NULL
159 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
160 || ( ld->ld_defconn = ldap_new_connection( ld, NULL, 1,0,0 )) == NULL
161 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
163 ldap_ld_free( ld, 0 );
167 ld->ld_sb.sb_useaddr = ld->ld_sb.sb_addrs[ 0 ];
168 cldap_setretryinfo( ld, 0, 0 );
172 for ( i = 0; i < ld->ld_sb.sb_naddr; ++i ) {
173 Debug( LDAP_DEBUG_TRACE, "end of cldap_open address %d is %s\n",
174 i, inet_ntoa( ((struct sockaddr_in *)
175 ld->ld_sb.sb_addrs[ i ])->sin_addr ), 0 );
185 cldap_close( LDAP *ld )
187 ldap_ld_free( ld, 0 );
192 cldap_setretryinfo( LDAP *ld, int tries, int timeout )
194 ld->ld_cldaptries = ( tries <= 0 ) ? DEF_CLDAP_TRIES : tries;
195 ld->ld_cldaptimeout = ( timeout <= 0 ) ? DEF_CLDAP_TIMEOUT : timeout;
200 cldap_search_s( LDAP *ld, char *base, int scope, char *filter, char **attrs,
201 int attrsonly, LDAPMessage **res, char *logdn )
204 struct cldap_retinfo cri;
208 (void) memset( &cri, 0, sizeof( cri ));
210 if ( logdn != NULL ) {
211 ld->ld_cldapdn = logdn;
212 } else if ( ld->ld_cldapdn == NULL ) {
217 if ( cri.cri_try != 0 ) {
218 --ld->ld_msgid; /* use same id as before */
220 ld->ld_sb.sb_useaddr = ld->ld_sb.sb_addrs[ cri.cri_useaddr ];
222 Debug( LDAP_DEBUG_TRACE, "cldap_search_s try %d (to %s)\n",
223 cri.cri_try, inet_ntoa( ((struct sockaddr_in *)
224 ld->ld_sb.sb_useaddr)->sin_addr ), 0 );
226 if ( (msgid = ldap_search( ld, base, scope, filter, attrs,
227 attrsonly )) == -1 ) {
228 return( ld->ld_errno );
231 if ( ld->ld_cache != NULL && ld->ld_responses != NULL ) {
232 Debug( LDAP_DEBUG_TRACE, "cldap_search_s res from cache\n",
234 *res = ld->ld_responses;
235 ld->ld_responses = ld->ld_responses->lm_next;
236 return( ldap_result2error( ld, *res, 0 ));
238 #endif /* LDAP_NOCACHE */
239 ret = cldap_result( ld, msgid, res, &cri, base );
247 add_addr( LDAP *ld, struct sockaddr *sap )
249 struct sockaddr *newsap, **addrs;
251 if (( newsap = (struct sockaddr *)malloc( sizeof( struct sockaddr )))
253 ld->ld_errno = LDAP_NO_MEMORY;
257 if ( ld->ld_sb.sb_naddr == 0 ) {
258 addrs = (struct sockaddr **)malloc( sizeof(struct sockaddr *));
260 addrs = (struct sockaddr **)realloc( ld->ld_sb.sb_addrs,
261 ( ld->ld_sb.sb_naddr + 1 ) * sizeof(struct sockaddr *));
264 if ( addrs == NULL ) {
266 ld->ld_errno = LDAP_NO_MEMORY;
270 SAFEMEMCPY( (char *)newsap, (char *)sap, sizeof( struct sockaddr ));
271 addrs[ ld->ld_sb.sb_naddr++ ] = newsap;
272 ld->ld_sb.sb_addrs = (void **)addrs;
278 cldap_result( LDAP *ld, int msgid, LDAPMessage **res,
279 struct cldap_retinfo *crip, char *base )
281 Sockbuf *sb = &ld->ld_sb;
284 int ret, id, fromaddr, i;
289 if ( crip->cri_try == 0 ) {
290 crip->cri_maxtries = ld->ld_cldaptries * sb->sb_naddr;
291 crip->cri_timeout = ld->ld_cldaptimeout;
292 crip->cri_useaddr = 0;
293 Debug( LDAP_DEBUG_TRACE, "cldap_result tries %d timeout %d\n",
294 ld->ld_cldaptries, ld->ld_cldaptimeout, 0 );
297 if ((tv.tv_sec = crip->cri_timeout / sb->sb_naddr) < 1 ) {
302 Debug( LDAP_DEBUG_TRACE,
303 "cldap_result waiting up to %ld seconds for a response\n",
304 (long) tv.tv_sec, 0, 0 );
305 ber_init_w_nullc( &ber, 0 );
306 ldap_set_ber_options( ld, &ber );
308 if ( cldap_getmsg( ld, &tv, &ber ) == -1 ) {
310 Debug( LDAP_DEBUG_TRACE, "cldap_getmsg returned -1 (%d)\n",
312 } else if ( ld->ld_errno == LDAP_TIMEOUT ) {
313 Debug( LDAP_DEBUG_TRACE,
314 "cldap_result timed out\n", 0, 0, 0 );
316 * It timed out; is it time to give up?
318 if ( ++crip->cri_try >= crip->cri_maxtries ) {
322 if ( ++crip->cri_useaddr >= sb->sb_naddr ) {
324 * new round: reset address to first one and
327 crip->cri_useaddr = 0;
328 crip->cri_timeout <<= 1;
335 * Got a response. It should look like:
336 * { msgid, logdn, { searchresponse...}}
340 if ( ber_scanf( &ber, "ia", &id, &logdn ) == LBER_ERROR ) {
341 free( ber.ber_buf ); /* gack! */
342 ret = LDAP_DECODING_ERROR;
343 Debug( LDAP_DEBUG_TRACE,
344 "cldap_result: ber_scanf returned LBER_ERROR (%d)\n",
346 } else if ( id != msgid ) {
347 free( ber.ber_buf ); /* gack! */
348 Debug( LDAP_DEBUG_TRACE,
349 "cldap_result: looking for msgid %d; got %d\n",
351 ret = -1; /* ignore and keep looking */
354 * got a result: determine which server it came from
355 * decode into ldap message chain
357 for ( fromaddr = 0; fromaddr < sb->sb_naddr; ++fromaddr ) {
358 if ( memcmp( &((struct sockaddr_in *)
359 sb->sb_addrs[ fromaddr ])->sin_addr,
360 &((struct sockaddr_in *)sb->sb_fromaddr)->sin_addr,
361 sizeof( struct in_addr )) == 0 ) {
365 ret = cldap_parsemsg( ld, msgid, &ber, res, base );
366 free( ber.ber_buf ); /* gack! */
367 Debug( LDAP_DEBUG_TRACE,
368 "cldap_result got result (%d)\n", ret, 0, 0 );
371 if ( logdn != NULL ) {
378 * If we are giving up (successfully or otherwise) then
379 * abandon any outstanding requests.
383 if ( i >= sb->sb_naddr ) {
384 i = sb->sb_naddr - 1;
387 for ( ; i >= 0; --i ) {
388 if ( i == fromaddr ) {
391 sb->sb_useaddr = sb->sb_addrs[ i ];
392 Debug( LDAP_DEBUG_TRACE, "cldap_result abandoning id %d (to %s)\n",
393 msgid, inet_ntoa( ((struct sockaddr_in *)
394 sb->sb_useaddr)->sin_addr ), 0 );
395 (void) ldap_abandon( ld, msgid );
399 return( ld->ld_errno = ret );
404 cldap_parsemsg( LDAP *ld, int msgid, BerElement *ber,
405 LDAPMessage **res, char *base )
407 unsigned long tag, len;
408 int baselen, slen, rc;
409 char *dn, *p, *cookie;
410 LDAPMessage *chain, *prev, *ldm;
413 rc = LDAP_DECODING_ERROR; /* pessimistic */
414 ldm = chain = prev = NULLMSG;
415 baselen = ( base == NULL ) ? 0 : strlen( base );
418 for ( tag = ber_first_element( ber, &len, &cookie );
419 tag != LBER_DEFAULT && rc != LDAP_SUCCESS;
420 tag = ber_next_element( ber, &len, cookie )) {
421 if (( ldm = (LDAPMessage *)calloc( 1, sizeof(LDAPMessage)))
422 == NULL || ( ldm->lm_ber = ldap_alloc_ber_with_options( ld ))
425 break; /* return w/error*/
427 ldm->lm_msgid = msgid;
428 ldm->lm_msgtype = tag;
430 if ( tag == LDAP_RES_SEARCH_RESULT ) {
431 Debug( LDAP_DEBUG_TRACE, "cldap_parsemsg got search result\n",
434 if ( ber_get_stringal( ber, &bv ) == LBER_DEFAULT ) {
435 break; /* return w/error */
438 if ( ber_printf( ldm->lm_ber, "to", tag, bv->bv_val,
439 bv->bv_len ) == -1 ) {
440 break; /* return w/error */
446 } else if ( tag == LDAP_RES_SEARCH_ENTRY ) {
447 if ( ber_scanf( ber, "{aO", &dn, &bv ) == LBER_ERROR ) {
448 break; /* return w/error */
450 Debug( LDAP_DEBUG_TRACE, "cldap_parsemsg entry %s\n", dn, 0, 0 );
451 if ( dn != NULL && *(dn + ( slen = strlen(dn)) - 1) == '*' &&
454 * substitute original searchbase for trailing '*'
456 if (( p = (char *)malloc( slen + baselen )) == NULL ) {
459 break; /* return w/error */
462 strcpy( p + slen - 1, base );
467 if ( ber_printf( ldm->lm_ber, "t{so}", tag, dn, bv->bv_val,
468 bv->bv_len ) == -1 ) {
469 break; /* return w/error */
476 } else if ( tag == LDAP_RES_SEARCH_REFERENCE ) {
479 Debug( LDAP_DEBUG_TRACE, "cldap_parsemsg got unknown tag %lu\n",
481 rc = LDAP_PROTOCOL_ERROR;
482 break; /* return w/error */
485 /* Reset message ber so we can read from it later. Gack! */
486 ldm->lm_ber->ber_end = ldm->lm_ber->ber_ptr;
487 ldm->lm_ber->ber_ptr = ldm->lm_ber->ber_buf;
490 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
491 fprintf( stderr, "cldap_parsemsg add message id %d type %d:\n",
492 ldm->lm_msgid, ldm->lm_msgtype );
493 lber_log_dump( LDAP_DEBUG_BER, ldap_debug, ldm->lm_ber, 1 );
495 #endif /* LDAP_DEBUG */
498 if ( ld->ld_cache != NULL ) {
499 ldap_add_result_to_cache( ld, ldm );
501 #endif /* LDAP_NOCACHE */
503 if ( chain == NULL ) {
506 prev->lm_chain = ldm;
512 /* dispose of any leftovers */
514 if ( ldm->lm_ber != NULLBER ) {
515 ber_free( ldm->lm_ber, 1 );
523 /* return chain, calling result2error if we got anything at all */
525 return(( *res == NULLMSG ) ? rc : ldap_result2error( ld, *res, 0 ));
527 #endif /* LDAP_CONNECTIONLESS */