2 * Copyright (c) 1990, 1994 Regents of the University of Michigan.
5 * cldap.c - synchronous, retrying interface to the cldap protocol
10 #ifdef LDAP_CONNECTIONLESS
13 static char copyright[] = "@(#) Copyright (c) 1990, 1994 Regents of the University of Michigan.\nAll rights reserved.\n";
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 Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
69 if ( (s = socket( AF_INET, SOCK_DGRAM, 0 )) < 0 ) {
73 sock.sin_addr.s_addr = 0;
74 sock.sin_family = AF_INET;
76 if ( bind(s, (struct sockaddr *) &sock, sizeof(sock)) < 0) {
81 if (( ld = ldap_init( host, port )) == NULL ) {
85 if ( (ld->ld_sb.sb_fromaddr = (void *) calloc( 1,
86 sizeof( struct sockaddr ))) == NULL ) {
92 ld->ld_sb.sb_naddr = 0;
93 ld->ld_version = LDAP_VERSION;
95 sock.sin_family = AF_INET;
96 sock.sin_port = htons( port );
99 * 'host' may be a space-separated list.
101 if ( host != NULL ) {
102 for ( ; host != NULL; host = p ) {
103 if (( p = strchr( host, ' ' )) != NULL ) {
104 for (*p++ = '\0'; *p == ' '; p++) {
109 address = inet_addr( host );
110 /* This was just a test for -1 until OSF1 let inet_addr return
111 unsigned int, which is narrower than 'unsigned long address' */
112 if ( address == 0xffffffff || address == (unsigned long) -1 ) {
113 if ( (hp = gethostbyname( host )) == NULL ) {
114 errno = EHOSTUNREACH;
118 for ( i = 0; hp->h_addr_list[ i ] != 0; ++i ) {
119 SAFEMEMCPY( (char *)&sock.sin_addr.s_addr,
120 (char *)hp->h_addr_list[ i ],
121 sizeof(sock.sin_addr.s_addr));
122 if ( add_addr( ld, (struct sockaddr *)&sock ) < 0 ) {
123 ldap_ld_free( ld, 1 );
129 sock.sin_addr.s_addr = address;
130 if ( add_addr( ld, (struct sockaddr *)&sock ) < 0 ) {
131 ldap_ld_free( ld, 1 );
136 if ( ld->ld_host == NULL ) {
137 ld->ld_host = ldap_strdup( host );
142 address = INADDR_LOOPBACK;
143 sock.sin_addr.s_addr = htonl( address );
144 if ( add_addr( ld, (struct sockaddr *)&sock ) < 0 ) {
145 ldap_ld_free( ld, 1 );
150 if ( ld->ld_sb.sb_addrs == NULL
151 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
152 || ( ld->ld_defconn = ldap_new_connection( ld, NULL, 1,0,0 )) == NULL
153 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
155 ldap_ld_free( ld, 0 );
159 ld->ld_sb.sb_useaddr = ld->ld_sb.sb_addrs[ 0 ];
160 cldap_setretryinfo( ld, 0, 0 );
164 for ( i = 0; i < ld->ld_sb.sb_naddr; ++i ) {
165 Debug( LDAP_DEBUG_TRACE, "end of cldap_open address %d is %s\n",
166 i, inet_ntoa( ((struct sockaddr_in *)
167 ld->ld_sb.sb_addrs[ i ])->sin_addr ), 0 );
177 cldap_close( LDAP *ld )
179 ldap_ld_free( ld, 0 );
184 cldap_setretryinfo( LDAP *ld, int tries, int timeout )
186 ld->ld_cldaptries = ( tries <= 0 ) ? DEF_CLDAP_TRIES : tries;
187 ld->ld_cldaptimeout = ( timeout <= 0 ) ? DEF_CLDAP_TIMEOUT : timeout;
192 cldap_search_s( LDAP *ld, char *base, int scope, char *filter, char **attrs,
193 int attrsonly, LDAPMessage **res, char *logdn )
196 struct cldap_retinfo cri;
200 (void) memset( &cri, 0, sizeof( cri ));
202 if ( logdn != NULL ) {
203 ld->ld_cldapdn = logdn;
204 } else if ( ld->ld_cldapdn == NULL ) {
209 if ( cri.cri_try != 0 ) {
210 --ld->ld_msgid; /* use same id as before */
212 ld->ld_sb.sb_useaddr = ld->ld_sb.sb_addrs[ cri.cri_useaddr ];
214 Debug( LDAP_DEBUG_TRACE, "cldap_search_s try %d (to %s)\n",
215 cri.cri_try, inet_ntoa( ((struct sockaddr_in *)
216 ld->ld_sb.sb_useaddr)->sin_addr ), 0 );
218 if ( (msgid = ldap_search( ld, base, scope, filter, attrs,
219 attrsonly )) == -1 ) {
220 return( ld->ld_errno );
223 if ( ld->ld_cache != NULL && ld->ld_responses != NULL ) {
224 Debug( LDAP_DEBUG_TRACE, "cldap_search_s res from cache\n",
226 *res = ld->ld_responses;
227 ld->ld_responses = ld->ld_responses->lm_next;
228 return( ldap_result2error( ld, *res, 0 ));
230 #endif /* LDAP_NOCACHE */
231 ret = cldap_result( ld, msgid, res, &cri, base );
239 add_addr( LDAP *ld, struct sockaddr *sap )
241 struct sockaddr *newsap, **addrs;
243 if (( newsap = (struct sockaddr *)malloc( sizeof( struct sockaddr )))
245 ld->ld_errno = LDAP_NO_MEMORY;
249 if ( ld->ld_sb.sb_naddr == 0 ) {
250 addrs = (struct sockaddr **)malloc( sizeof(struct sockaddr *));
252 addrs = (struct sockaddr **)realloc( ld->ld_sb.sb_addrs,
253 ( ld->ld_sb.sb_naddr + 1 ) * sizeof(struct sockaddr *));
256 if ( addrs == NULL ) {
258 ld->ld_errno = LDAP_NO_MEMORY;
262 SAFEMEMCPY( (char *)newsap, (char *)sap, sizeof( struct sockaddr ));
263 addrs[ ld->ld_sb.sb_naddr++ ] = newsap;
264 ld->ld_sb.sb_addrs = (void **)addrs;
270 cldap_result( LDAP *ld, int msgid, LDAPMessage **res,
271 struct cldap_retinfo *crip, char *base )
273 Sockbuf *sb = &ld->ld_sb;
276 int ret, id, fromaddr, i;
281 if ( crip->cri_try == 0 ) {
282 crip->cri_maxtries = ld->ld_cldaptries * sb->sb_naddr;
283 crip->cri_timeout = ld->ld_cldaptimeout;
284 crip->cri_useaddr = 0;
285 Debug( LDAP_DEBUG_TRACE, "cldap_result tries %d timeout %d\n",
286 ld->ld_cldaptries, ld->ld_cldaptimeout, 0 );
289 if ((tv.tv_sec = crip->cri_timeout / sb->sb_naddr) < 1 ) {
294 Debug( LDAP_DEBUG_TRACE,
295 "cldap_result waiting up to %ld seconds for a response\n",
296 (long) tv.tv_sec, 0, 0 );
297 ber_init_w_nullc( &ber, 0 );
298 ldap_set_ber_options( ld, &ber );
300 if ( cldap_getmsg( ld, &tv, &ber ) == -1 ) {
302 Debug( LDAP_DEBUG_TRACE, "cldap_getmsg returned -1 (%d)\n",
304 } else if ( ld->ld_errno == LDAP_TIMEOUT ) {
305 Debug( LDAP_DEBUG_TRACE,
306 "cldap_result timed out\n", 0, 0, 0 );
308 * It timed out; is it time to give up?
310 if ( ++crip->cri_try >= crip->cri_maxtries ) {
314 if ( ++crip->cri_useaddr >= sb->sb_naddr ) {
316 * new round: reset address to first one and
319 crip->cri_useaddr = 0;
320 crip->cri_timeout <<= 1;
327 * Got a response. It should look like:
328 * { msgid, logdn, { searchresponse...}}
332 if ( ber_scanf( &ber, "ia", &id, &logdn ) == LBER_ERROR ) {
333 free( ber.ber_buf ); /* gack! */
334 ret = LDAP_DECODING_ERROR;
335 Debug( LDAP_DEBUG_TRACE,
336 "cldap_result: ber_scanf returned LBER_ERROR (%d)\n",
338 } else if ( id != msgid ) {
339 free( ber.ber_buf ); /* gack! */
340 Debug( LDAP_DEBUG_TRACE,
341 "cldap_result: looking for msgid %d; got %d\n",
343 ret = -1; /* ignore and keep looking */
346 * got a result: determine which server it came from
347 * decode into ldap message chain
349 for ( fromaddr = 0; fromaddr < sb->sb_naddr; ++fromaddr ) {
350 if ( memcmp( &((struct sockaddr_in *)
351 sb->sb_addrs[ fromaddr ])->sin_addr,
352 &((struct sockaddr_in *)sb->sb_fromaddr)->sin_addr,
353 sizeof( struct in_addr )) == 0 ) {
357 ret = cldap_parsemsg( ld, msgid, &ber, res, base );
358 free( ber.ber_buf ); /* gack! */
359 Debug( LDAP_DEBUG_TRACE,
360 "cldap_result got result (%d)\n", ret, 0, 0 );
363 if ( logdn != NULL ) {
370 * If we are giving up (successfully or otherwise) then
371 * abandon any outstanding requests.
375 if ( i >= sb->sb_naddr ) {
376 i = sb->sb_naddr - 1;
379 for ( ; i >= 0; --i ) {
380 if ( i == fromaddr ) {
383 sb->sb_useaddr = sb->sb_addrs[ i ];
384 Debug( LDAP_DEBUG_TRACE, "cldap_result abandoning id %d (to %s)\n",
385 msgid, inet_ntoa( ((struct sockaddr_in *)
386 sb->sb_useaddr)->sin_addr ), 0 );
387 (void) ldap_abandon( ld, msgid );
391 return( ld->ld_errno = ret );
396 cldap_parsemsg( LDAP *ld, int msgid, BerElement *ber,
397 LDAPMessage **res, char *base )
399 unsigned long tag, len;
400 int baselen, slen, rc;
401 char *dn, *p, *cookie;
402 LDAPMessage *chain, *prev, *ldm;
405 rc = LDAP_DECODING_ERROR; /* pessimistic */
406 ldm = chain = prev = NULLMSG;
407 baselen = ( base == NULL ) ? 0 : strlen( base );
410 for ( tag = ber_first_element( ber, &len, &cookie );
411 tag != LBER_DEFAULT && rc != LDAP_SUCCESS;
412 tag = ber_next_element( ber, &len, cookie )) {
413 if (( ldm = (LDAPMessage *)calloc( 1, sizeof(LDAPMessage)))
414 == NULL || ( ldm->lm_ber = ldap_alloc_ber_with_options( ld ))
417 break; /* return w/error*/
419 ldm->lm_msgid = msgid;
420 ldm->lm_msgtype = tag;
422 if ( tag == LDAP_RES_SEARCH_RESULT ) {
423 Debug( LDAP_DEBUG_TRACE, "cldap_parsemsg got search result\n",
426 if ( ber_get_stringal( ber, &bv ) == LBER_DEFAULT ) {
427 break; /* return w/error */
430 if ( ber_printf( ldm->lm_ber, "to", tag, bv->bv_val,
431 bv->bv_len ) == -1 ) {
432 break; /* return w/error */
438 } else if ( tag == LDAP_RES_SEARCH_ENTRY ) {
439 if ( ber_scanf( ber, "{aO", &dn, &bv ) == LBER_ERROR ) {
440 break; /* return w/error */
442 Debug( LDAP_DEBUG_TRACE, "cldap_parsemsg entry %s\n", dn, 0, 0 );
443 if ( dn != NULL && *(dn + ( slen = strlen(dn)) - 1) == '*' &&
446 * substitute original searchbase for trailing '*'
448 if (( p = (char *)malloc( slen + baselen )) == NULL ) {
451 break; /* return w/error */
454 strcpy( p + slen - 1, base );
459 if ( ber_printf( ldm->lm_ber, "t{so}", tag, dn, bv->bv_val,
460 bv->bv_len ) == -1 ) {
461 break; /* return w/error */
468 Debug( LDAP_DEBUG_TRACE, "cldap_parsemsg got unknown tag %lu\n",
470 rc = LDAP_PROTOCOL_ERROR;
471 break; /* return w/error */
474 /* Reset message ber so we can read from it later. Gack! */
475 ldm->lm_ber->ber_end = ldm->lm_ber->ber_ptr;
476 ldm->lm_ber->ber_ptr = ldm->lm_ber->ber_buf;
479 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
480 fprintf( stderr, "cldap_parsemsg add message id %d type %d:\n",
481 ldm->lm_msgid, ldm->lm_msgtype );
482 ber_dump( ldm->lm_ber, 1 );
484 #endif /* LDAP_DEBUG */
487 if ( ld->ld_cache != NULL ) {
488 ldap_add_result_to_cache( ld, ldm );
490 #endif /* LDAP_NOCACHE */
492 if ( chain == NULL ) {
495 prev->lm_chain = ldm;
501 /* dispose of any leftovers */
503 if ( ldm->lm_ber != NULLBER ) {
504 ber_free( ldm->lm_ber, 1 );
512 /* return chain, calling result2error if we got anything at all */
514 return(( *res == NULLMSG ) ? rc : ldap_result2error( ld, *res, 0 ));
516 #endif /* LDAP_CONNECTIONLESS */