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 || ( ld->ld_defconn = ldap_new_connection( ld, NULL, 1,0,0 )) == NULL
161 ldap_ld_free( ld, 0 );
165 ber_pvt_sb_udp_set_dst( &ld->ld_sb, ld->ld_cldapaddrs[0] );
167 cldap_setretryinfo( ld, 0, 0 );
171 for ( i = 0; i < ld->ld_cldapnaddr; ++i ) {
172 Debug( LDAP_DEBUG_TRACE, "end of cldap_open address %d is %s\n",
173 i, inet_ntoa( ((struct sockaddr_in *)
174 ld->ld_cldapaddrs[ i ])->sin_addr ), 0 );
184 cldap_close( LDAP *ld )
186 ldap_ld_free( ld, 0 );
191 cldap_setretryinfo( LDAP *ld, int tries, int timeout )
193 ld->ld_cldaptries = ( tries <= 0 ) ? DEF_CLDAP_TRIES : tries;
194 ld->ld_cldaptimeout = ( timeout <= 0 ) ? DEF_CLDAP_TIMEOUT : timeout;
199 cldap_search_s( LDAP *ld, char *base, int scope, char *filter, char **attrs,
200 int attrsonly, LDAPMessage **res, char *logdn )
203 struct cldap_retinfo cri;
207 (void) memset( &cri, 0, sizeof( cri ));
209 if ( logdn != NULL ) {
210 ld->ld_cldapdn = logdn;
211 } else if ( ld->ld_cldapdn == NULL ) {
216 if ( cri.cri_try != 0 ) {
217 --ld->ld_msgid; /* use same id as before */
220 ber_pvt_sb_udp_set_dst( &(ld->ld_sb),
221 ld->ld_cldapaddrs[ cri.cri_useaddr ] );
223 Debug( LDAP_DEBUG_TRACE, "cldap_search_s try %d (to %s)\n",
224 cri.cri_try, inet_ntoa( ((struct sockaddr_in *)
225 ld->ld_cldapaddrs[ cri.cri_useaddr ])->sin_addr), 0 );
227 if ( (msgid = ldap_search( ld, base, scope, filter, attrs,
228 attrsonly )) == -1 ) {
229 return( ld->ld_errno );
232 if ( ld->ld_cache != NULL && ld->ld_responses != NULL ) {
233 Debug( LDAP_DEBUG_TRACE, "cldap_search_s res from cache\n",
235 *res = ld->ld_responses;
236 ld->ld_responses = ld->ld_responses->lm_next;
237 return( ldap_result2error( ld, *res, 0 ));
239 #endif /* LDAP_NOCACHE */
240 ret = cldap_result( ld, msgid, res, &cri, base );
248 add_addr( LDAP *ld, struct sockaddr *sap )
250 struct sockaddr *newsap, **addrs;
252 if (( newsap = (struct sockaddr *)malloc( sizeof( struct sockaddr )))
254 ld->ld_errno = LDAP_NO_MEMORY;
258 if ( ld->ld_cldapnaddr == 0 ) {
259 addrs = (struct sockaddr **)malloc( sizeof(struct sockaddr *));
261 addrs = (struct sockaddr **)realloc( ld->ld_cldapaddrs,
262 ( ld->ld_cldapnaddr + 1 ) * sizeof(struct sockaddr *));
265 if ( addrs == NULL ) {
267 ld->ld_errno = LDAP_NO_MEMORY;
271 SAFEMEMCPY( (char *)newsap, (char *)sap, sizeof( struct sockaddr ));
272 addrs[ ld->ld_cldapnaddr++ ] = newsap;
273 ld->ld_cldapaddrs = (void **)addrs;
279 cldap_result( LDAP *ld, int msgid, LDAPMessage **res,
280 struct cldap_retinfo *crip, char *base )
282 Sockbuf *sb = &ld->ld_sb;
285 int ret, id, fromaddr, i;
290 if ( crip->cri_try == 0 ) {
291 crip->cri_maxtries = ld->ld_cldaptries * ld->ld_cldapnaddr;
292 crip->cri_timeout = ld->ld_cldaptimeout;
293 crip->cri_useaddr = 0;
294 Debug( LDAP_DEBUG_TRACE, "cldap_result tries %d timeout %d\n",
295 ld->ld_cldaptries, ld->ld_cldaptimeout, 0 );
298 if ((tv.tv_sec = crip->cri_timeout / ld->ld_cldapnaddr) < 1 ) {
303 Debug( LDAP_DEBUG_TRACE,
304 "cldap_result waiting up to %ld seconds for a response\n",
305 (long) tv.tv_sec, 0, 0 );
306 ber_init_w_nullc( &ber, 0 );
307 ldap_set_ber_options( ld, &ber );
309 if ( cldap_getmsg( ld, &tv, &ber ) == -1 ) {
311 Debug( LDAP_DEBUG_TRACE, "cldap_getmsg returned -1 (%d)\n",
313 } else if ( ld->ld_errno == LDAP_TIMEOUT ) {
314 Debug( LDAP_DEBUG_TRACE,
315 "cldap_result timed out\n", 0, 0, 0 );
317 * It timed out; is it time to give up?
319 if ( ++crip->cri_try >= crip->cri_maxtries ) {
323 if ( ++crip->cri_useaddr >= ld->ld_cldapnaddr ) {
325 * new round: reset address to first one and
328 crip->cri_useaddr = 0;
329 crip->cri_timeout <<= 1;
336 * Got a response. It should look like:
337 * { msgid, logdn, { searchresponse...}}
341 if ( ber_scanf( &ber, "ia", &id, &logdn ) == LBER_ERROR ) {
342 free( ber.ber_buf ); /* gack! */
343 ret = LDAP_DECODING_ERROR;
344 Debug( LDAP_DEBUG_TRACE,
345 "cldap_result: ber_scanf returned LBER_ERROR (%d)\n",
347 } else if ( id != msgid ) {
348 free( ber.ber_buf ); /* gack! */
349 Debug( LDAP_DEBUG_TRACE,
350 "cldap_result: looking for msgid %d; got %d\n",
352 ret = -1; /* ignore and keep looking */
354 struct sockaddr_in * src;
356 * got a result: determine which server it came from
357 * decode into ldap message chain
359 src = (struct sockaddr_in *) ber_pvt_sb_udp_get_src( sb );
361 for ( fromaddr = 0; fromaddr < ld->ld_cldapnaddr; ++fromaddr ) {
362 if ( memcmp( &((struct sockaddr_in *)
363 ld->ld_cldapaddrs[ fromaddr ])->sin_addr,
365 sizeof( struct in_addr )) == 0 ) {
369 ret = cldap_parsemsg( ld, msgid, &ber, res, base );
370 free( ber.ber_buf ); /* gack! */
371 Debug( LDAP_DEBUG_TRACE,
372 "cldap_result got result (%d)\n", ret, 0, 0 );
375 if ( logdn != NULL ) {
382 * If we are giving up (successfully or otherwise) then
383 * abandon any outstanding requests.
387 if ( i >= ld->ld_cldapnaddr ) {
388 i = ld->ld_cldapnaddr - 1;
391 for ( ; i >= 0; --i ) {
392 if ( i == fromaddr ) {
395 ber_pvt_sb_udp_set_dst( sb, ld->ld_cldapaddrs[i] );
397 Debug( LDAP_DEBUG_TRACE, "cldap_result abandoning id %d (to %s)\n",
398 msgid, inet_ntoa( ((struct sockaddr_in *)
399 ld->ld_cldapaddrs[i])->sin_addr ), 0 );
400 (void) ldap_abandon( ld, msgid );
404 return( ld->ld_errno = ret );
409 cldap_parsemsg( LDAP *ld, int msgid, BerElement *ber,
410 LDAPMessage **res, char *base )
412 unsigned long tag, len;
413 int baselen, slen, rc;
414 char *dn, *p, *cookie;
415 LDAPMessage *chain, *prev, *ldm;
418 rc = LDAP_DECODING_ERROR; /* pessimistic */
419 ldm = chain = prev = NULLMSG;
420 baselen = ( base == NULL ) ? 0 : strlen( base );
423 for ( tag = ber_first_element( ber, &len, &cookie );
424 tag != LBER_DEFAULT && rc != LDAP_SUCCESS;
425 tag = ber_next_element( ber, &len, cookie )) {
426 if (( ldm = (LDAPMessage *)calloc( 1, sizeof(LDAPMessage)))
427 == NULL || ( ldm->lm_ber = ldap_alloc_ber_with_options( ld ))
430 break; /* return w/error*/
432 ldm->lm_msgid = msgid;
433 ldm->lm_msgtype = tag;
435 if ( tag == LDAP_RES_SEARCH_RESULT ) {
436 Debug( LDAP_DEBUG_TRACE, "cldap_parsemsg got search result\n",
439 if ( ber_get_stringal( ber, &bv ) == LBER_DEFAULT ) {
440 break; /* return w/error */
443 if ( ber_printf( ldm->lm_ber, "to", tag, bv->bv_val,
444 bv->bv_len ) == -1 ) {
445 break; /* return w/error */
451 } else if ( tag == LDAP_RES_SEARCH_ENTRY ) {
452 if ( ber_scanf( ber, "{aO", &dn, &bv ) == LBER_ERROR ) {
453 break; /* return w/error */
455 Debug( LDAP_DEBUG_TRACE, "cldap_parsemsg entry %s\n", dn, 0, 0 );
456 if ( dn != NULL && *(dn + ( slen = strlen(dn)) - 1) == '*' &&
459 * substitute original searchbase for trailing '*'
461 if (( p = (char *)malloc( slen + baselen )) == NULL ) {
464 break; /* return w/error */
467 strcpy( p + slen - 1, base );
472 if ( ber_printf( ldm->lm_ber, "t{so}", tag, dn, bv->bv_val,
473 bv->bv_len ) == -1 ) {
474 break; /* return w/error */
481 } else if ( tag == LDAP_RES_SEARCH_REFERENCE ) {
484 Debug( LDAP_DEBUG_TRACE, "cldap_parsemsg got unknown tag %lu\n",
486 rc = LDAP_PROTOCOL_ERROR;
487 break; /* return w/error */
490 /* Reset message ber so we can read from it later. Gack! */
491 ldm->lm_ber->ber_end = ldm->lm_ber->ber_ptr;
492 ldm->lm_ber->ber_ptr = ldm->lm_ber->ber_buf;
495 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
496 fprintf( stderr, "cldap_parsemsg add message id %d type %d:\n",
497 ldm->lm_msgid, ldm->lm_msgtype );
498 ber_log_dump( LDAP_DEBUG_BER, ldap_debug, ldm->lm_ber, 1 );
500 #endif /* LDAP_DEBUG */
503 if ( ld->ld_cache != NULL ) {
504 ldap_add_result_to_cache( ld, ldm );
506 #endif /* LDAP_NOCACHE */
508 if ( chain == NULL ) {
511 prev->lm_chain = ldm;
517 /* dispose of any leftovers */
519 if ( ldm->lm_ber != NULLBER ) {
520 ber_free( ldm->lm_ber, 1 );
528 /* return chain, calling result2error if we got anything at all */
530 return(( *res == NULLMSG ) ? rc : ldap_result2error( ld, *res, 0 ));
532 #endif /* LDAP_CONNECTIONLESS */