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>
28 #define DEF_CLDAP_TIMEOUT 3
29 #define DEF_CLDAP_TRIES 4
32 struct cldap_retinfo {
39 static int add_addr LDAP_P((
40 LDAP *ld, struct sockaddr *sap ));
41 static int cldap_result LDAP_P((
42 LDAP *ld, int msgid, LDAPMessage **res,
43 struct cldap_retinfo *crip, char *base ));
44 static int cldap_parsemsg LDAP_P((
45 LDAP *ld, int msgid, BerElement *ber,
46 LDAPMessage **res, char *base ));
49 * cldap_open - initialize and connect to an ldap server. A magic cookie to
50 * be used for future communication is returned on success, NULL on failure.
54 * ld = cldap_open( hostname, port );
58 cldap_open( char *host, int port )
61 unsigned long address;
62 struct sockaddr_in sock;
68 Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
74 if ( (s = socket( AF_INET, SOCK_DGRAM, 0 )) < 0 ) {
78 sock.sin_addr.s_addr = 0;
79 sock.sin_family = AF_INET;
81 if ( bind(s, (struct sockaddr *) &sock, sizeof(sock)) < 0) {
86 if (( ld = ldap_init( host, port )) == NULL ) {
90 if ( (ld->ld_sb.sb_fromaddr = (void *) calloc( 1,
91 sizeof( struct sockaddr ))) == NULL ) {
97 ld->ld_sb.sb_naddr = 0;
98 ld->ld_version = LDAP_VERSION;
100 sock.sin_family = AF_INET;
101 sock.sin_port = htons( port );
104 * 'host' may be a space-separated list.
106 if ( host != NULL ) {
107 for ( ; host != NULL; host = p ) {
108 if (( p = strchr( host, ' ' )) != NULL ) {
109 for (*p++ = '\0'; *p == ' '; p++) {
114 if ( (address = inet_addr( host )) == -1 ) {
115 if ( (hp = gethostbyname( host )) == NULL ) {
116 errno = EHOSTUNREACH;
120 for ( i = 0; hp->h_addr_list[ i ] != 0; ++i ) {
121 SAFEMEMCPY( (char *)&sock.sin_addr.s_addr,
122 (char *)hp->h_addr_list[ i ],
123 sizeof(sock.sin_addr.s_addr));
124 if ( add_addr( ld, (struct sockaddr *)&sock ) < 0 ) {
132 sock.sin_addr.s_addr = address;
133 if ( add_addr( ld, (struct sockaddr *)&sock ) < 0 ) {
140 if ( ld->ld_host == NULL ) {
141 ld->ld_host = strdup( host );
146 address = INADDR_LOOPBACK;
147 sock.sin_addr.s_addr = htonl( address );
148 if ( add_addr( ld, (struct sockaddr *)&sock ) < 0 ) {
155 if ( ld->ld_sb.sb_addrs == NULL
156 #ifdef LDAP_REFERRALS
157 || ( ld->ld_defconn = ldap_new_connection( ld, NULL, 1,0,0 )) == NULL
158 #endif /* LDAP_REFERRALS */
164 ld->ld_sb.sb_useaddr = ld->ld_sb.sb_addrs[ 0 ];
165 cldap_setretryinfo( ld, 0, 0 );
169 for ( i = 0; i < ld->ld_sb.sb_naddr; ++i ) {
170 Debug( LDAP_DEBUG_TRACE, "end of cldap_open address %d is %s\n",
171 i, inet_ntoa( ((struct sockaddr_in *)
172 ld->ld_sb.sb_addrs[ i ])->sin_addr ), 0 );
182 cldap_close( LDAP *ld )
184 ldap_ld_free( ld, 0 );
189 cldap_setretryinfo( LDAP *ld, int tries, int timeout )
191 ld->ld_cldaptries = ( tries <= 0 ) ? DEF_CLDAP_TRIES : tries;
192 ld->ld_cldaptimeout = ( timeout <= 0 ) ? DEF_CLDAP_TIMEOUT : timeout;
197 cldap_search_s( LDAP *ld, char *base, int scope, char *filter, char **attrs,
198 int attrsonly, LDAPMessage **res, char *logdn )
201 struct cldap_retinfo cri;
205 (void) memset( &cri, 0, sizeof( cri ));
207 if ( logdn != NULL ) {
208 ld->ld_cldapdn = logdn;
209 } else if ( ld->ld_cldapdn == NULL ) {
214 if ( cri.cri_try != 0 ) {
215 --ld->ld_msgid; /* use same id as before */
217 ld->ld_sb.sb_useaddr = ld->ld_sb.sb_addrs[ cri.cri_useaddr ];
219 Debug( LDAP_DEBUG_TRACE, "cldap_search_s try %d (to %s)\n",
220 cri.cri_try, inet_ntoa( ((struct sockaddr_in *)
221 ld->ld_sb.sb_useaddr)->sin_addr ), 0 );
223 if ( (msgid = ldap_search( ld, base, scope, filter, attrs,
224 attrsonly )) == -1 ) {
225 return( ld->ld_errno );
228 if ( ld->ld_cache != NULL && ld->ld_responses != NULL ) {
229 Debug( LDAP_DEBUG_TRACE, "cldap_search_s res from cache\n",
231 *res = ld->ld_responses;
232 ld->ld_responses = ld->ld_responses->lm_next;
233 return( ldap_result2error( ld, *res, 0 ));
235 #endif /* LDAP_NOCACHE */
236 ret = cldap_result( ld, msgid, res, &cri, base );
244 add_addr( LDAP *ld, struct sockaddr *sap )
246 struct sockaddr *newsap, **addrs;
248 if (( newsap = (struct sockaddr *)malloc( sizeof( struct sockaddr )))
250 ld->ld_errno = LDAP_NO_MEMORY;
254 if ( ld->ld_sb.sb_naddr == 0 ) {
255 addrs = (struct sockaddr **)malloc( sizeof(struct sockaddr *));
257 addrs = (struct sockaddr **)realloc( ld->ld_sb.sb_addrs,
258 ( ld->ld_sb.sb_naddr + 1 ) * sizeof(struct sockaddr *));
261 if ( addrs == NULL ) {
263 ld->ld_errno = LDAP_NO_MEMORY;
267 SAFEMEMCPY( (char *)newsap, (char *)sap, sizeof( struct sockaddr ));
268 addrs[ ld->ld_sb.sb_naddr++ ] = newsap;
269 ld->ld_sb.sb_addrs = (void **)addrs;
275 cldap_result( LDAP *ld, int msgid, LDAPMessage **res,
276 struct cldap_retinfo *crip, char *base )
278 Sockbuf *sb = &ld->ld_sb;
281 int ret, id, fromaddr, i;
286 if ( crip->cri_try == 0 ) {
287 crip->cri_maxtries = ld->ld_cldaptries * sb->sb_naddr;
288 crip->cri_timeout = ld->ld_cldaptimeout;
289 crip->cri_useaddr = 0;
290 Debug( LDAP_DEBUG_TRACE, "cldap_result tries %d timeout %d\n",
291 ld->ld_cldaptries, ld->ld_cldaptimeout, 0 );
294 if ((tv.tv_sec = crip->cri_timeout / sb->sb_naddr) < 1 ) {
299 Debug( LDAP_DEBUG_TRACE,
300 "cldap_result waiting up to %d seconds for a response\n",
303 ldap_set_ber_options( ld, &ber );
305 if ( cldap_getmsg( ld, &tv, &ber ) == -1 ) {
307 Debug( LDAP_DEBUG_TRACE, "cldap_getmsg returned -1 (%d)\n",
309 } else if ( ld->ld_errno == LDAP_TIMEOUT ) {
310 Debug( LDAP_DEBUG_TRACE,
311 "cldap_result timed out\n", 0, 0, 0 );
313 * It timed out; is it time to give up?
315 if ( ++crip->cri_try >= crip->cri_maxtries ) {
319 if ( ++crip->cri_useaddr >= sb->sb_naddr ) {
321 * new round: reset address to first one and
324 crip->cri_useaddr = 0;
325 crip->cri_timeout <<= 1;
332 * Got a response. It should look like:
333 * { msgid, logdn, { searchresponse...}}
337 if ( ber_scanf( &ber, "ia", &id, &logdn ) == LBER_ERROR ) {
338 free( ber.ber_buf ); /* gack! */
339 ret = LDAP_DECODING_ERROR;
340 Debug( LDAP_DEBUG_TRACE,
341 "cldap_result: ber_scanf returned LBER_ERROR (%d)\n",
343 } else if ( id != msgid ) {
344 free( ber.ber_buf ); /* gack! */
345 Debug( LDAP_DEBUG_TRACE,
346 "cldap_result: looking for msgid %d; got %d\n",
348 ret = -1; /* ignore and keep looking */
351 * got a result: determine which server it came from
352 * decode into ldap message chain
354 for ( fromaddr = 0; fromaddr < sb->sb_naddr; ++fromaddr ) {
355 if ( memcmp( &((struct sockaddr_in *)
356 sb->sb_addrs[ fromaddr ])->sin_addr,
357 &((struct sockaddr_in *)sb->sb_fromaddr)->sin_addr,
358 sizeof( struct in_addr )) == 0 ) {
362 ret = cldap_parsemsg( ld, msgid, &ber, res, base );
363 free( ber.ber_buf ); /* gack! */
364 Debug( LDAP_DEBUG_TRACE,
365 "cldap_result got result (%d)\n", ret, 0, 0 );
368 if ( logdn != NULL ) {
375 * If we are giving up (successfully or otherwise) then
376 * abandon any outstanding requests.
380 if ( i >= sb->sb_naddr ) {
381 i = sb->sb_naddr - 1;
384 for ( ; i >= 0; --i ) {
385 if ( i == fromaddr ) {
388 sb->sb_useaddr = sb->sb_addrs[ i ];
389 Debug( LDAP_DEBUG_TRACE, "cldap_result abandoning id %d (to %s)\n",
390 msgid, inet_ntoa( ((struct sockaddr_in *)
391 sb->sb_useaddr)->sin_addr ), 0 );
392 (void) ldap_abandon( ld, msgid );
396 return( ld->ld_errno = ret );
401 cldap_parsemsg( LDAP *ld, int msgid, BerElement *ber,
402 LDAPMessage **res, char *base )
404 unsigned long tag, len;
405 int baselen, slen, rc;
406 char *dn, *p, *cookie;
407 LDAPMessage *chain, *prev, *ldm;
410 rc = LDAP_DECODING_ERROR; /* pessimistic */
411 ldm = chain = prev = NULLMSG;
412 baselen = ( base == NULL ) ? 0 : strlen( base );
415 for ( tag = ber_first_element( ber, &len, &cookie );
416 tag != LBER_DEFAULT && rc != LDAP_SUCCESS;
417 tag = ber_next_element( ber, &len, cookie )) {
418 if (( ldm = (LDAPMessage *)calloc( 1, sizeof(LDAPMessage)))
419 == NULL || ( ldm->lm_ber = ldap_alloc_ber_with_options( ld ))
422 break; /* return w/error*/
424 ldm->lm_msgid = msgid;
425 ldm->lm_msgtype = tag;
427 if ( tag == LDAP_RES_SEARCH_RESULT ) {
428 Debug( LDAP_DEBUG_TRACE, "cldap_parsemsg got search result\n",
431 if ( ber_get_stringal( ber, &bv ) == LBER_DEFAULT ) {
432 break; /* return w/error */
435 if ( ber_printf( ldm->lm_ber, "to", tag, bv->bv_val,
436 bv->bv_len ) == -1 ) {
437 break; /* return w/error */
443 } else if ( tag == LDAP_RES_SEARCH_ENTRY ) {
444 if ( ber_scanf( ber, "{aO", &dn, &bv ) == LBER_ERROR ) {
445 break; /* return w/error */
447 Debug( LDAP_DEBUG_TRACE, "cldap_parsemsg entry %s\n", dn, 0, 0 );
448 if ( dn != NULL && *(dn + ( slen = strlen(dn)) - 1) == '*' &&
451 * substitute original searchbase for trailing '*'
453 if (( p = (char *)malloc( slen + baselen )) == NULL ) {
456 break; /* return w/error */
459 strcpy( p + slen - 1, base );
464 if ( ber_printf( ldm->lm_ber, "t{so}", tag, dn, bv->bv_val,
465 bv->bv_len ) == -1 ) {
466 break; /* return w/error */
473 Debug( LDAP_DEBUG_TRACE, "cldap_parsemsg got unknown tag %d\n",
475 rc = LDAP_PROTOCOL_ERROR;
476 break; /* return w/error */
479 /* Reset message ber so we can read from it later. Gack! */
480 ldm->lm_ber->ber_end = ldm->lm_ber->ber_ptr;
481 ldm->lm_ber->ber_ptr = ldm->lm_ber->ber_buf;
484 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
485 fprintf( stderr, "cldap_parsemsg add message id %d type %d:\n",
486 ldm->lm_msgid, ldm->lm_msgtype );
487 ber_dump( ldm->lm_ber, 1 );
489 #endif /* LDAP_DEBUG */
492 if ( ld->ld_cache != NULL ) {
493 ldap_add_result_to_cache( ld, ldm );
495 #endif /* LDAP_NOCACHE */
497 if ( chain == NULL ) {
500 prev->lm_chain = ldm;
506 /* dispose of any leftovers */
508 if ( ldm->lm_ber != NULLBER ) {
509 ber_free( ldm->lm_ber, 1 );
517 /* return chain, calling result2error if we got anything at all */
519 return(( *res == NULLMSG ) ? rc : ldap_result2error( ld, *res, 0 ));
521 #endif /* LDAP_CONNECTIONLESS */