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) {
87 if (( ld = ldap_init( host, port )) == NULL ) {
92 ld->ld_cldapnaddr = 0;
93 ld->ld_cldapaddrs = NULL;
95 if (ber_pvt_sb_set_io( &(ld->ld_sb), &ber_pvt_sb_io_udp, NULL )<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_cldapaddrs == 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 ber_pvt_sb_udp_set_dst( &ld->ld_sb, ld->ld_cldapaddrs[0] );
169 cldap_setretryinfo( ld, 0, 0 );
173 for ( i = 0; i < ld->ld_cldapnaddr; ++i ) {
174 Debug( LDAP_DEBUG_TRACE, "end of cldap_open address %d is %s\n",
175 i, inet_ntoa( ((struct sockaddr_in *)
176 ld->ld_cldapaddrs[ i ])->sin_addr ), 0 );
186 cldap_close( LDAP *ld )
188 ldap_ld_free( ld, 0 );
193 cldap_setretryinfo( LDAP *ld, int tries, int timeout )
195 ld->ld_cldaptries = ( tries <= 0 ) ? DEF_CLDAP_TRIES : tries;
196 ld->ld_cldaptimeout = ( timeout <= 0 ) ? DEF_CLDAP_TIMEOUT : timeout;
201 cldap_search_s( LDAP *ld, char *base, int scope, char *filter, char **attrs,
202 int attrsonly, LDAPMessage **res, char *logdn )
205 struct cldap_retinfo cri;
209 (void) memset( &cri, 0, sizeof( cri ));
211 if ( logdn != NULL ) {
212 ld->ld_cldapdn = logdn;
213 } else if ( ld->ld_cldapdn == NULL ) {
218 if ( cri.cri_try != 0 ) {
219 --ld->ld_msgid; /* use same id as before */
222 ber_pvt_sb_udp_set_dst( &(ld->ld_sb),
223 ld->ld_cldapaddrs[ cri.cri_useaddr ] );
225 Debug( LDAP_DEBUG_TRACE, "cldap_search_s try %d (to %s)\n",
226 cri.cri_try, inet_ntoa( ((struct sockaddr_in *)
227 ld->ld_cldapaddrs[ cri.cri_useaddr ])->sin_addr), 0 );
229 if ( (msgid = ldap_search( ld, base, scope, filter, attrs,
230 attrsonly )) == -1 ) {
231 return( ld->ld_errno );
234 if ( ld->ld_cache != NULL && ld->ld_responses != NULL ) {
235 Debug( LDAP_DEBUG_TRACE, "cldap_search_s res from cache\n",
237 *res = ld->ld_responses;
238 ld->ld_responses = ld->ld_responses->lm_next;
239 return( ldap_result2error( ld, *res, 0 ));
241 #endif /* LDAP_NOCACHE */
242 ret = cldap_result( ld, msgid, res, &cri, base );
250 add_addr( LDAP *ld, struct sockaddr *sap )
252 struct sockaddr *newsap, **addrs;
254 if (( newsap = (struct sockaddr *)malloc( sizeof( struct sockaddr )))
256 ld->ld_errno = LDAP_NO_MEMORY;
260 if ( ld->ld_cldapnaddr == 0 ) {
261 addrs = (struct sockaddr **)malloc( sizeof(struct sockaddr *));
263 addrs = (struct sockaddr **)realloc( ld->ld_cldapaddrs,
264 ( ld->ld_cldapnaddr + 1 ) * sizeof(struct sockaddr *));
267 if ( addrs == NULL ) {
269 ld->ld_errno = LDAP_NO_MEMORY;
273 SAFEMEMCPY( (char *)newsap, (char *)sap, sizeof( struct sockaddr ));
274 addrs[ ld->ld_cldapnaddr++ ] = newsap;
275 ld->ld_cldapaddrs = (void **)addrs;
281 cldap_result( LDAP *ld, int msgid, LDAPMessage **res,
282 struct cldap_retinfo *crip, char *base )
284 Sockbuf *sb = &ld->ld_sb;
287 int ret, id, fromaddr, i;
292 if ( crip->cri_try == 0 ) {
293 crip->cri_maxtries = ld->ld_cldaptries * ld->ld_cldapnaddr;
294 crip->cri_timeout = ld->ld_cldaptimeout;
295 crip->cri_useaddr = 0;
296 Debug( LDAP_DEBUG_TRACE, "cldap_result tries %d timeout %d\n",
297 ld->ld_cldaptries, ld->ld_cldaptimeout, 0 );
300 if ((tv.tv_sec = crip->cri_timeout / ld->ld_cldapnaddr) < 1 ) {
305 Debug( LDAP_DEBUG_TRACE,
306 "cldap_result waiting up to %ld seconds for a response\n",
307 (long) tv.tv_sec, 0, 0 );
308 ber_init_w_nullc( &ber, 0 );
309 ldap_set_ber_options( ld, &ber );
311 if ( cldap_getmsg( ld, &tv, &ber ) == -1 ) {
313 Debug( LDAP_DEBUG_TRACE, "cldap_getmsg returned -1 (%d)\n",
315 } else if ( ld->ld_errno == LDAP_TIMEOUT ) {
316 Debug( LDAP_DEBUG_TRACE,
317 "cldap_result timed out\n", 0, 0, 0 );
319 * It timed out; is it time to give up?
321 if ( ++crip->cri_try >= crip->cri_maxtries ) {
325 if ( ++crip->cri_useaddr >= ld->ld_cldapnaddr ) {
327 * new round: reset address to first one and
330 crip->cri_useaddr = 0;
331 crip->cri_timeout <<= 1;
338 * Got a response. It should look like:
339 * { msgid, logdn, { searchresponse...}}
343 if ( ber_scanf( &ber, "ia", &id, &logdn ) == LBER_ERROR ) {
344 free( ber.ber_buf ); /* gack! */
345 ret = LDAP_DECODING_ERROR;
346 Debug( LDAP_DEBUG_TRACE,
347 "cldap_result: ber_scanf returned LBER_ERROR (%d)\n",
349 } else if ( id != msgid ) {
350 free( ber.ber_buf ); /* gack! */
351 Debug( LDAP_DEBUG_TRACE,
352 "cldap_result: looking for msgid %d; got %d\n",
354 ret = -1; /* ignore and keep looking */
356 struct sockaddr_in * src;
358 * got a result: determine which server it came from
359 * decode into ldap message chain
361 src = (struct sockaddr_in *) ber_pvt_sb_udp_get_src( sb );
363 for ( fromaddr = 0; fromaddr < ld->ld_cldapnaddr; ++fromaddr ) {
364 if ( memcmp( &((struct sockaddr_in *)
365 ld->ld_cldapaddrs[ fromaddr ])->sin_addr,
367 sizeof( struct in_addr )) == 0 ) {
371 ret = cldap_parsemsg( ld, msgid, &ber, res, base );
372 free( ber.ber_buf ); /* gack! */
373 Debug( LDAP_DEBUG_TRACE,
374 "cldap_result got result (%d)\n", ret, 0, 0 );
377 if ( logdn != NULL ) {
384 * If we are giving up (successfully or otherwise) then
385 * abandon any outstanding requests.
389 if ( i >= ld->ld_cldapnaddr ) {
390 i = ld->ld_cldapnaddr - 1;
393 for ( ; i >= 0; --i ) {
394 if ( i == fromaddr ) {
397 ber_pvt_sb_udp_set_dst( sb, ld->ld_cldapaddrs[i] );
399 Debug( LDAP_DEBUG_TRACE, "cldap_result abandoning id %d (to %s)\n",
400 msgid, inet_ntoa( ((struct sockaddr_in *)
401 ld->ld_cldapaddrs[i])->sin_addr ), 0 );
402 (void) ldap_abandon( ld, msgid );
406 return( ld->ld_errno = ret );
411 cldap_parsemsg( LDAP *ld, int msgid, BerElement *ber,
412 LDAPMessage **res, char *base )
414 unsigned long tag, len;
415 int baselen, slen, rc;
416 char *dn, *p, *cookie;
417 LDAPMessage *chain, *prev, *ldm;
420 rc = LDAP_DECODING_ERROR; /* pessimistic */
421 ldm = chain = prev = NULLMSG;
422 baselen = ( base == NULL ) ? 0 : strlen( base );
425 for ( tag = ber_first_element( ber, &len, &cookie );
426 tag != LBER_DEFAULT && rc != LDAP_SUCCESS;
427 tag = ber_next_element( ber, &len, cookie )) {
428 if (( ldm = (LDAPMessage *)calloc( 1, sizeof(LDAPMessage)))
429 == NULL || ( ldm->lm_ber = ldap_alloc_ber_with_options( ld ))
432 break; /* return w/error*/
434 ldm->lm_msgid = msgid;
435 ldm->lm_msgtype = tag;
437 if ( tag == LDAP_RES_SEARCH_RESULT ) {
438 Debug( LDAP_DEBUG_TRACE, "cldap_parsemsg got search result\n",
441 if ( ber_get_stringal( ber, &bv ) == LBER_DEFAULT ) {
442 break; /* return w/error */
445 if ( ber_printf( ldm->lm_ber, "to", tag, bv->bv_val,
446 bv->bv_len ) == -1 ) {
447 break; /* return w/error */
453 } else if ( tag == LDAP_RES_SEARCH_ENTRY ) {
454 if ( ber_scanf( ber, "{aO", &dn, &bv ) == LBER_ERROR ) {
455 break; /* return w/error */
457 Debug( LDAP_DEBUG_TRACE, "cldap_parsemsg entry %s\n", dn, 0, 0 );
458 if ( dn != NULL && *(dn + ( slen = strlen(dn)) - 1) == '*' &&
461 * substitute original searchbase for trailing '*'
463 if (( p = (char *)malloc( slen + baselen )) == NULL ) {
466 break; /* return w/error */
469 strcpy( p + slen - 1, base );
474 if ( ber_printf( ldm->lm_ber, "t{so}", tag, dn, bv->bv_val,
475 bv->bv_len ) == -1 ) {
476 break; /* return w/error */
483 } else if ( tag == LDAP_RES_SEARCH_REFERENCE ) {
486 Debug( LDAP_DEBUG_TRACE, "cldap_parsemsg got unknown tag %lu\n",
488 rc = LDAP_PROTOCOL_ERROR;
489 break; /* return w/error */
492 /* Reset message ber so we can read from it later. Gack! */
493 ldm->lm_ber->ber_end = ldm->lm_ber->ber_ptr;
494 ldm->lm_ber->ber_ptr = ldm->lm_ber->ber_buf;
497 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
498 fprintf( stderr, "cldap_parsemsg add message id %d type %d:\n",
499 ldm->lm_msgid, ldm->lm_msgtype );
500 ber_log_dump( LDAP_DEBUG_BER, ldap_debug, ldm->lm_ber, 1 );
502 #endif /* LDAP_DEBUG */
505 if ( ld->ld_cache != NULL ) {
506 ldap_add_result_to_cache( ld, ldm );
508 #endif /* LDAP_NOCACHE */
510 if ( chain == NULL ) {
513 prev->lm_chain = ldm;
519 /* dispose of any leftovers */
521 if ( ldm->lm_ber != NULLBER ) {
522 ber_free( ldm->lm_ber, 1 );
530 /* return chain, calling result2error if we got anything at all */
532 return(( *res == NULLMSG ) ? rc : ldap_result2error( ld, *res, 0 ));
534 #endif /* LDAP_CONNECTIONLESS */