2 * Copyright (c) 1990, 1994 Regents of the University of Michigan.
5 * cldap.c - synchronous, retrying interface to the cldap protocol
14 static char copyright[] = "@(#) Copyright (c) 1990, 1994 Regents of the University of Michigan.\nAll rights reserved.\n";
18 #include <ac/string.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
39 #define DEF_CLDAP_TIMEOUT 3
40 #define DEF_CLDAP_TRIES 4
42 #ifndef INADDR_LOOPBACK
43 #define INADDR_LOOPBACK ((unsigned long) 0x7f000001)
47 struct cldap_retinfo {
55 static int add_addr( LDAP *ld, struct sockaddr *sap );
56 static int cldap_result( LDAP *ld, int msgid, LDAPMessage **res,
57 struct cldap_retinfo *crip, char *base );
58 static int cldap_parsemsg( LDAP *ld, int msgid, BerElement *ber,
59 LDAPMessage **res, char *base );
60 #else /* NEEDPROTOS */
61 static int add_addr();
62 static int cldap_result();
63 static int cldap_parsemsg();
64 #endif /* NEEDPROTOS */
67 * cldap_open - initialize and connect to an ldap server. A magic cookie to
68 * be used for future communication is returned on success, NULL on failure.
72 * ld = cldap_open( hostname, port );
76 cldap_open( char *host, int port )
79 unsigned long address;
80 struct sockaddr_in sock;
86 Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
92 if ( (s = socket( AF_INET, SOCK_DGRAM, 0 )) < 0 ) {
96 sock.sin_addr.s_addr = 0;
97 sock.sin_family = AF_INET;
99 if ( bind(s, (struct sockaddr *) &sock, sizeof(sock)) < 0) {
104 if (( ld = ldap_init( host, port )) == NULL ) {
108 if ( (ld->ld_sb.sb_fromaddr = (void *) calloc( 1,
109 sizeof( struct sockaddr ))) == NULL ) {
115 ld->ld_sb.sb_naddr = 0;
116 ld->ld_version = LDAP_VERSION;
118 sock.sin_family = AF_INET;
119 sock.sin_port = htons( port );
122 * 'host' may be a space-separated list.
124 if ( host != NULL ) {
125 for ( ; host != NULL; host = p ) {
126 if (( p = strchr( host, ' ' )) != NULL ) {
127 for (*p++ = '\0'; *p == ' '; p++) {
132 if ( (address = inet_addr( host )) == -1 ) {
133 if ( (hp = gethostbyname( host )) == NULL ) {
134 errno = EHOSTUNREACH;
138 for ( i = 0; hp->h_addr_list[ i ] != 0; ++i ) {
139 SAFEMEMCPY( (char *)&sock.sin_addr.s_addr,
140 (char *)hp->h_addr_list[ i ],
141 sizeof(sock.sin_addr.s_addr));
142 if ( add_addr( ld, (struct sockaddr *)&sock ) < 0 ) {
150 sock.sin_addr.s_addr = address;
151 if ( add_addr( ld, (struct sockaddr *)&sock ) < 0 ) {
158 if ( ld->ld_host == NULL ) {
159 ld->ld_host = strdup( host );
164 address = INADDR_LOOPBACK;
165 sock.sin_addr.s_addr = htonl( address );
166 if ( add_addr( ld, (struct sockaddr *)&sock ) < 0 ) {
173 if ( ld->ld_sb.sb_addrs == NULL
174 #ifdef LDAP_REFERRALS
175 || ( ld->ld_defconn = ldap_new_connection( ld, NULL, 1,0,0 )) == NULL
176 #endif /* LDAP_REFERRALS */
182 ld->ld_sb.sb_useaddr = ld->ld_sb.sb_addrs[ 0 ];
183 cldap_setretryinfo( ld, 0, 0 );
187 for ( i = 0; i < ld->ld_sb.sb_naddr; ++i ) {
188 Debug( LDAP_DEBUG_TRACE, "end of cldap_open address %d is %s\n",
189 i, inet_ntoa( ((struct sockaddr_in *)
190 ld->ld_sb.sb_addrs[ i ])->sin_addr ), 0 );
200 cldap_close( LDAP *ld )
202 ldap_ld_free( ld, 0 );
207 cldap_setretryinfo( LDAP *ld, int tries, int timeout )
209 ld->ld_cldaptries = ( tries <= 0 ) ? DEF_CLDAP_TRIES : tries;
210 ld->ld_cldaptimeout = ( timeout <= 0 ) ? DEF_CLDAP_TIMEOUT : timeout;
215 cldap_search_s( LDAP *ld, char *base, int scope, char *filter, char **attrs,
216 int attrsonly, LDAPMessage **res, char *logdn )
219 struct cldap_retinfo cri;
223 (void) memset( &cri, 0, sizeof( cri ));
225 if ( logdn != NULL ) {
226 ld->ld_cldapdn = logdn;
227 } else if ( ld->ld_cldapdn == NULL ) {
232 if ( cri.cri_try != 0 ) {
233 --ld->ld_msgid; /* use same id as before */
235 ld->ld_sb.sb_useaddr = ld->ld_sb.sb_addrs[ cri.cri_useaddr ];
237 Debug( LDAP_DEBUG_TRACE, "cldap_search_s try %d (to %s)\n",
238 cri.cri_try, inet_ntoa( ((struct sockaddr_in *)
239 ld->ld_sb.sb_useaddr)->sin_addr ), 0 );
241 if ( (msgid = ldap_search( ld, base, scope, filter, attrs,
242 attrsonly )) == -1 ) {
243 return( ld->ld_errno );
246 if ( ld->ld_cache != NULL && ld->ld_responses != NULL ) {
247 Debug( LDAP_DEBUG_TRACE, "cldap_search_s res from cache\n",
249 *res = ld->ld_responses;
250 ld->ld_responses = ld->ld_responses->lm_next;
251 return( ldap_result2error( ld, *res, 0 ));
253 #endif /* NO_CACHE */
254 ret = cldap_result( ld, msgid, res, &cri, base );
262 add_addr( LDAP *ld, struct sockaddr *sap )
264 struct sockaddr *newsap, **addrs;
266 if (( newsap = (struct sockaddr *)malloc( sizeof( struct sockaddr )))
268 ld->ld_errno = LDAP_NO_MEMORY;
272 if ( ld->ld_sb.sb_naddr == 0 ) {
273 addrs = (struct sockaddr **)malloc( sizeof(struct sockaddr *));
275 addrs = (struct sockaddr **)realloc( ld->ld_sb.sb_addrs,
276 ( ld->ld_sb.sb_naddr + 1 ) * sizeof(struct sockaddr *));
279 if ( addrs == NULL ) {
281 ld->ld_errno = LDAP_NO_MEMORY;
285 SAFEMEMCPY( (char *)newsap, (char *)sap, sizeof( struct sockaddr ));
286 addrs[ ld->ld_sb.sb_naddr++ ] = newsap;
287 ld->ld_sb.sb_addrs = (void **)addrs;
293 cldap_result( LDAP *ld, int msgid, LDAPMessage **res,
294 struct cldap_retinfo *crip, char *base )
296 Sockbuf *sb = &ld->ld_sb;
299 int ret, id, fromaddr, i;
304 if ( crip->cri_try == 0 ) {
305 crip->cri_maxtries = ld->ld_cldaptries * sb->sb_naddr;
306 crip->cri_timeout = ld->ld_cldaptimeout;
307 crip->cri_useaddr = 0;
308 Debug( LDAP_DEBUG_TRACE, "cldap_result tries %d timeout %d\n",
309 ld->ld_cldaptries, ld->ld_cldaptimeout, 0 );
312 if ((tv.tv_sec = crip->cri_timeout / sb->sb_naddr) < 1 ) {
317 Debug( LDAP_DEBUG_TRACE,
318 "cldap_result waiting up to %d seconds for a response\n",
321 ldap_set_ber_options( ld, &ber );
323 if ( cldap_getmsg( ld, &tv, &ber ) == -1 ) {
325 Debug( LDAP_DEBUG_TRACE, "cldap_getmsg returned -1 (%d)\n",
327 } else if ( ld->ld_errno == LDAP_TIMEOUT ) {
328 Debug( LDAP_DEBUG_TRACE,
329 "cldap_result timed out\n", 0, 0, 0 );
331 * It timed out; is it time to give up?
333 if ( ++crip->cri_try >= crip->cri_maxtries ) {
337 if ( ++crip->cri_useaddr >= sb->sb_naddr ) {
339 * new round: reset address to first one and
342 crip->cri_useaddr = 0;
343 crip->cri_timeout <<= 1;
350 * Got a response. It should look like:
351 * { msgid, logdn, { searchresponse...}}
355 if ( ber_scanf( &ber, "ia", &id, &logdn ) == LBER_ERROR ) {
356 free( ber.ber_buf ); /* gack! */
357 ret = LDAP_DECODING_ERROR;
358 Debug( LDAP_DEBUG_TRACE,
359 "cldap_result: ber_scanf returned LBER_ERROR (%d)\n",
361 } else if ( id != msgid ) {
362 free( ber.ber_buf ); /* gack! */
363 Debug( LDAP_DEBUG_TRACE,
364 "cldap_result: looking for msgid %d; got %d\n",
366 ret = -1; /* ignore and keep looking */
369 * got a result: determine which server it came from
370 * decode into ldap message chain
372 for ( fromaddr = 0; fromaddr < sb->sb_naddr; ++fromaddr ) {
373 if ( memcmp( &((struct sockaddr_in *)
374 sb->sb_addrs[ fromaddr ])->sin_addr,
375 &((struct sockaddr_in *)sb->sb_fromaddr)->sin_addr,
376 sizeof( struct in_addr )) == 0 ) {
380 ret = cldap_parsemsg( ld, msgid, &ber, res, base );
381 free( ber.ber_buf ); /* gack! */
382 Debug( LDAP_DEBUG_TRACE,
383 "cldap_result got result (%d)\n", ret, 0, 0 );
386 if ( logdn != NULL ) {
393 * If we are giving up (successfully or otherwise) then
394 * abandon any outstanding requests.
398 if ( i >= sb->sb_naddr ) {
399 i = sb->sb_naddr - 1;
402 for ( ; i >= 0; --i ) {
403 if ( i == fromaddr ) {
406 sb->sb_useaddr = sb->sb_addrs[ i ];
407 Debug( LDAP_DEBUG_TRACE, "cldap_result abandoning id %d (to %s)\n",
408 msgid, inet_ntoa( ((struct sockaddr_in *)
409 sb->sb_useaddr)->sin_addr ), 0 );
410 (void) ldap_abandon( ld, msgid );
414 return( ld->ld_errno = ret );
419 cldap_parsemsg( LDAP *ld, int msgid, BerElement *ber,
420 LDAPMessage **res, char *base )
422 unsigned long tag, len;
423 int baselen, slen, rc;
424 char *dn, *p, *cookie;
425 LDAPMessage *chain, *prev, *ldm;
428 rc = LDAP_DECODING_ERROR; /* pessimistic */
429 ldm = chain = prev = NULLMSG;
430 baselen = ( base == NULL ) ? 0 : strlen( base );
433 for ( tag = ber_first_element( ber, &len, &cookie );
434 tag != LBER_DEFAULT && rc != LDAP_SUCCESS;
435 tag = ber_next_element( ber, &len, cookie )) {
436 if (( ldm = (LDAPMessage *)calloc( 1, sizeof(LDAPMessage)))
437 == NULL || ( ldm->lm_ber = ldap_alloc_ber_with_options( ld ))
440 break; /* return w/error*/
442 ldm->lm_msgid = msgid;
443 ldm->lm_msgtype = tag;
445 if ( tag == LDAP_RES_SEARCH_RESULT ) {
446 Debug( LDAP_DEBUG_TRACE, "cldap_parsemsg got search result\n",
449 if ( ber_get_stringal( ber, &bv ) == LBER_DEFAULT ) {
450 break; /* return w/error */
453 if ( ber_printf( ldm->lm_ber, "to", tag, bv->bv_val,
454 bv->bv_len ) == -1 ) {
455 break; /* return w/error */
461 } else if ( tag == LDAP_RES_SEARCH_ENTRY ) {
462 if ( ber_scanf( ber, "{aO", &dn, &bv ) == LBER_ERROR ) {
463 break; /* return w/error */
465 Debug( LDAP_DEBUG_TRACE, "cldap_parsemsg entry %s\n", dn, 0, 0 );
466 if ( dn != NULL && *(dn + ( slen = strlen(dn)) - 1) == '*' &&
469 * substitute original searchbase for trailing '*'
471 if (( p = (char *)malloc( slen + baselen )) == NULL ) {
474 break; /* return w/error */
477 strcpy( p + slen - 1, base );
482 if ( ber_printf( ldm->lm_ber, "t{so}", tag, dn, bv->bv_val,
483 bv->bv_len ) == -1 ) {
484 break; /* return w/error */
491 Debug( LDAP_DEBUG_TRACE, "cldap_parsemsg got unknown tag %d\n",
493 rc = LDAP_PROTOCOL_ERROR;
494 break; /* return w/error */
497 /* Reset message ber so we can read from it later. Gack! */
498 ldm->lm_ber->ber_end = ldm->lm_ber->ber_ptr;
499 ldm->lm_ber->ber_ptr = ldm->lm_ber->ber_buf;
502 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
503 fprintf( stderr, "cldap_parsemsg add message id %d type %d:\n",
504 ldm->lm_msgid, ldm->lm_msgtype );
505 ber_dump( ldm->lm_ber, 1 );
507 #endif /* LDAP_DEBUG */
510 if ( ld->ld_cache != NULL ) {
511 ldap_add_result_to_cache( ld, ldm );
513 #endif /* NO_CACHE */
515 if ( chain == NULL ) {
518 prev->lm_chain = ldm;
524 /* dispose of any leftovers */
526 if ( ldm->lm_ber != NULLBER ) {
527 ber_free( ldm->lm_ber, 1 );
535 /* return chain, calling result2error if we got anything at all */
537 return(( *res == NULLMSG ) ? rc : ldap_result2error( ld, *res, 0 ));