2 * Copyright (c) 1990, 1994 Regents of the University of Michigan.
5 * cldap.c - synchronous, retrying interface to the cldap protocol
13 static char copyright[] = "@(#) Copyright (c) 1990, 1994 Regents of the University of Michigan.\nAll rights reserved.\n";
21 #include <ac/socket.h>
22 #include <ac/string.h>
28 #define DEF_CLDAP_TIMEOUT 3
29 #define DEF_CLDAP_TRIES 4
31 #ifndef INADDR_LOOPBACK
32 #define INADDR_LOOPBACK ((unsigned long) 0x7f000001)
36 struct cldap_retinfo {
44 static int add_addr( LDAP *ld, struct sockaddr *sap );
45 static int cldap_result( LDAP *ld, int msgid, LDAPMessage **res,
46 struct cldap_retinfo *crip, char *base );
47 static int cldap_parsemsg( LDAP *ld, int msgid, BerElement *ber,
48 LDAPMessage **res, char *base );
49 #else /* NEEDPROTOS */
50 static int add_addr();
51 static int cldap_result();
52 static int cldap_parsemsg();
53 #endif /* NEEDPROTOS */
56 * cldap_open - initialize and connect to an ldap server. A magic cookie to
57 * be used for future communication is returned on success, NULL on failure.
61 * ld = cldap_open( hostname, port );
65 cldap_open( char *host, int port )
68 unsigned long address;
69 struct sockaddr_in sock;
75 Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
81 if ( (s = socket( AF_INET, SOCK_DGRAM, 0 )) < 0 ) {
85 sock.sin_addr.s_addr = 0;
86 sock.sin_family = AF_INET;
88 if ( bind(s, (struct sockaddr *) &sock, sizeof(sock)) < 0) {
93 if (( ld = ldap_init( host, port )) == NULL ) {
97 if ( (ld->ld_sb.sb_fromaddr = (void *) calloc( 1,
98 sizeof( struct sockaddr ))) == NULL ) {
104 ld->ld_sb.sb_naddr = 0;
105 ld->ld_version = LDAP_VERSION;
107 sock.sin_family = AF_INET;
108 sock.sin_port = htons( port );
111 * 'host' may be a space-separated list.
113 if ( host != NULL ) {
114 for ( ; host != NULL; host = p ) {
115 if (( p = strchr( host, ' ' )) != NULL ) {
116 for (*p++ = '\0'; *p == ' '; p++) {
121 if ( (address = inet_addr( host )) == -1 ) {
122 if ( (hp = gethostbyname( host )) == NULL ) {
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 ) {
139 sock.sin_addr.s_addr = address;
140 if ( add_addr( ld, (struct sockaddr *)&sock ) < 0 ) {
147 if ( ld->ld_host == NULL ) {
148 ld->ld_host = strdup( host );
153 address = INADDR_LOOPBACK;
154 sock.sin_addr.s_addr = htonl( address );
155 if ( add_addr( ld, (struct sockaddr *)&sock ) < 0 ) {
162 if ( ld->ld_sb.sb_addrs == NULL
163 #ifdef LDAP_REFERRALS
164 || ( ld->ld_defconn = ldap_new_connection( ld, NULL, 1,0,0 )) == NULL
165 #endif /* LDAP_REFERRALS */
171 ld->ld_sb.sb_useaddr = ld->ld_sb.sb_addrs[ 0 ];
172 cldap_setretryinfo( ld, 0, 0 );
176 for ( i = 0; i < ld->ld_sb.sb_naddr; ++i ) {
177 Debug( LDAP_DEBUG_TRACE, "end of cldap_open address %d is %s\n",
178 i, inet_ntoa( ((struct sockaddr_in *)
179 ld->ld_sb.sb_addrs[ i ])->sin_addr ), 0 );
189 cldap_close( LDAP *ld )
191 ldap_ld_free( ld, 0 );
196 cldap_setretryinfo( LDAP *ld, int tries, int timeout )
198 ld->ld_cldaptries = ( tries <= 0 ) ? DEF_CLDAP_TRIES : tries;
199 ld->ld_cldaptimeout = ( timeout <= 0 ) ? DEF_CLDAP_TIMEOUT : timeout;
204 cldap_search_s( LDAP *ld, char *base, int scope, char *filter, char **attrs,
205 int attrsonly, LDAPMessage **res, char *logdn )
208 struct cldap_retinfo cri;
212 (void) memset( &cri, 0, sizeof( cri ));
214 if ( logdn != NULL ) {
215 ld->ld_cldapdn = logdn;
216 } else if ( ld->ld_cldapdn == NULL ) {
221 if ( cri.cri_try != 0 ) {
222 --ld->ld_msgid; /* use same id as before */
224 ld->ld_sb.sb_useaddr = ld->ld_sb.sb_addrs[ cri.cri_useaddr ];
226 Debug( LDAP_DEBUG_TRACE, "cldap_search_s try %d (to %s)\n",
227 cri.cri_try, inet_ntoa( ((struct sockaddr_in *)
228 ld->ld_sb.sb_useaddr)->sin_addr ), 0 );
230 if ( (msgid = ldap_search( ld, base, scope, filter, attrs,
231 attrsonly )) == -1 ) {
232 return( ld->ld_errno );
235 if ( ld->ld_cache != NULL && ld->ld_responses != NULL ) {
236 Debug( LDAP_DEBUG_TRACE, "cldap_search_s res from cache\n",
238 *res = ld->ld_responses;
239 ld->ld_responses = ld->ld_responses->lm_next;
240 return( ldap_result2error( ld, *res, 0 ));
242 #endif /* NO_CACHE */
243 ret = cldap_result( ld, msgid, res, &cri, base );
251 add_addr( LDAP *ld, struct sockaddr *sap )
253 struct sockaddr *newsap, **addrs;
255 if (( newsap = (struct sockaddr *)malloc( sizeof( struct sockaddr )))
257 ld->ld_errno = LDAP_NO_MEMORY;
261 if ( ld->ld_sb.sb_naddr == 0 ) {
262 addrs = (struct sockaddr **)malloc( sizeof(struct sockaddr *));
264 addrs = (struct sockaddr **)realloc( ld->ld_sb.sb_addrs,
265 ( ld->ld_sb.sb_naddr + 1 ) * sizeof(struct sockaddr *));
268 if ( addrs == NULL ) {
270 ld->ld_errno = LDAP_NO_MEMORY;
274 SAFEMEMCPY( (char *)newsap, (char *)sap, sizeof( struct sockaddr ));
275 addrs[ ld->ld_sb.sb_naddr++ ] = newsap;
276 ld->ld_sb.sb_addrs = (void **)addrs;
282 cldap_result( LDAP *ld, int msgid, LDAPMessage **res,
283 struct cldap_retinfo *crip, char *base )
285 Sockbuf *sb = &ld->ld_sb;
288 int ret, id, fromaddr, i;
293 if ( crip->cri_try == 0 ) {
294 crip->cri_maxtries = ld->ld_cldaptries * sb->sb_naddr;
295 crip->cri_timeout = ld->ld_cldaptimeout;
296 crip->cri_useaddr = 0;
297 Debug( LDAP_DEBUG_TRACE, "cldap_result tries %d timeout %d\n",
298 ld->ld_cldaptries, ld->ld_cldaptimeout, 0 );
301 if ((tv.tv_sec = crip->cri_timeout / sb->sb_naddr) < 1 ) {
306 Debug( LDAP_DEBUG_TRACE,
307 "cldap_result waiting up to %d seconds for a response\n",
310 ldap_set_ber_options( ld, &ber );
312 if ( cldap_getmsg( ld, &tv, &ber ) == -1 ) {
314 Debug( LDAP_DEBUG_TRACE, "cldap_getmsg returned -1 (%d)\n",
316 } else if ( ld->ld_errno == LDAP_TIMEOUT ) {
317 Debug( LDAP_DEBUG_TRACE,
318 "cldap_result timed out\n", 0, 0, 0 );
320 * It timed out; is it time to give up?
322 if ( ++crip->cri_try >= crip->cri_maxtries ) {
326 if ( ++crip->cri_useaddr >= sb->sb_naddr ) {
328 * new round: reset address to first one and
331 crip->cri_useaddr = 0;
332 crip->cri_timeout <<= 1;
339 * Got a response. It should look like:
340 * { msgid, logdn, { searchresponse...}}
344 if ( ber_scanf( &ber, "ia", &id, &logdn ) == LBER_ERROR ) {
345 free( ber.ber_buf ); /* gack! */
346 ret = LDAP_DECODING_ERROR;
347 Debug( LDAP_DEBUG_TRACE,
348 "cldap_result: ber_scanf returned LBER_ERROR (%d)\n",
350 } else if ( id != msgid ) {
351 free( ber.ber_buf ); /* gack! */
352 Debug( LDAP_DEBUG_TRACE,
353 "cldap_result: looking for msgid %d; got %d\n",
355 ret = -1; /* ignore and keep looking */
358 * got a result: determine which server it came from
359 * decode into ldap message chain
361 for ( fromaddr = 0; fromaddr < sb->sb_naddr; ++fromaddr ) {
362 if ( memcmp( &((struct sockaddr_in *)
363 sb->sb_addrs[ fromaddr ])->sin_addr,
364 &((struct sockaddr_in *)sb->sb_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 >= sb->sb_naddr ) {
388 i = sb->sb_naddr - 1;
391 for ( ; i >= 0; --i ) {
392 if ( i == fromaddr ) {
395 sb->sb_useaddr = sb->sb_addrs[ i ];
396 Debug( LDAP_DEBUG_TRACE, "cldap_result abandoning id %d (to %s)\n",
397 msgid, inet_ntoa( ((struct sockaddr_in *)
398 sb->sb_useaddr)->sin_addr ), 0 );
399 (void) ldap_abandon( ld, msgid );
403 return( ld->ld_errno = ret );
408 cldap_parsemsg( LDAP *ld, int msgid, BerElement *ber,
409 LDAPMessage **res, char *base )
411 unsigned long tag, len;
412 int baselen, slen, rc;
413 char *dn, *p, *cookie;
414 LDAPMessage *chain, *prev, *ldm;
417 rc = LDAP_DECODING_ERROR; /* pessimistic */
418 ldm = chain = prev = NULLMSG;
419 baselen = ( base == NULL ) ? 0 : strlen( base );
422 for ( tag = ber_first_element( ber, &len, &cookie );
423 tag != LBER_DEFAULT && rc != LDAP_SUCCESS;
424 tag = ber_next_element( ber, &len, cookie )) {
425 if (( ldm = (LDAPMessage *)calloc( 1, sizeof(LDAPMessage)))
426 == NULL || ( ldm->lm_ber = ldap_alloc_ber_with_options( ld ))
429 break; /* return w/error*/
431 ldm->lm_msgid = msgid;
432 ldm->lm_msgtype = tag;
434 if ( tag == LDAP_RES_SEARCH_RESULT ) {
435 Debug( LDAP_DEBUG_TRACE, "cldap_parsemsg got search result\n",
438 if ( ber_get_stringal( ber, &bv ) == LBER_DEFAULT ) {
439 break; /* return w/error */
442 if ( ber_printf( ldm->lm_ber, "to", tag, bv->bv_val,
443 bv->bv_len ) == -1 ) {
444 break; /* return w/error */
450 } else if ( tag == LDAP_RES_SEARCH_ENTRY ) {
451 if ( ber_scanf( ber, "{aO", &dn, &bv ) == LBER_ERROR ) {
452 break; /* return w/error */
454 Debug( LDAP_DEBUG_TRACE, "cldap_parsemsg entry %s\n", dn, 0, 0 );
455 if ( dn != NULL && *(dn + ( slen = strlen(dn)) - 1) == '*' &&
458 * substitute original searchbase for trailing '*'
460 if (( p = (char *)malloc( slen + baselen )) == NULL ) {
463 break; /* return w/error */
466 strcpy( p + slen - 1, base );
471 if ( ber_printf( ldm->lm_ber, "t{so}", tag, dn, bv->bv_val,
472 bv->bv_len ) == -1 ) {
473 break; /* return w/error */
480 Debug( LDAP_DEBUG_TRACE, "cldap_parsemsg got unknown tag %d\n",
482 rc = LDAP_PROTOCOL_ERROR;
483 break; /* return w/error */
486 /* Reset message ber so we can read from it later. Gack! */
487 ldm->lm_ber->ber_end = ldm->lm_ber->ber_ptr;
488 ldm->lm_ber->ber_ptr = ldm->lm_ber->ber_buf;
491 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
492 fprintf( stderr, "cldap_parsemsg add message id %d type %d:\n",
493 ldm->lm_msgid, ldm->lm_msgtype );
494 ber_dump( ldm->lm_ber, 1 );
496 #endif /* LDAP_DEBUG */
499 if ( ld->ld_cache != NULL ) {
500 ldap_add_result_to_cache( ld, ldm );
502 #endif /* NO_CACHE */
504 if ( chain == NULL ) {
507 prev->lm_chain = ldm;
513 /* dispose of any leftovers */
515 if ( ldm->lm_ber != NULLBER ) {
516 ber_free( ldm->lm_ber, 1 );
524 /* return chain, calling result2error if we got anything at all */
526 return(( *res == NULLMSG ) ? rc : ldap_result2error( ld, *res, 0 ));