2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 * Copyright (c) 1990 Regents of the University of Michigan.
9 * result.c - wait for an ldap result
18 #include <ac/socket.h>
19 #include <ac/string.h>
21 #include <ac/unistd.h>
26 static int ldap_abandoned LDAP_P(( LDAP *ld, int msgid ));
27 static int ldap_mark_abandoned LDAP_P(( LDAP *ld, int msgid ));
28 static int wait4msg LDAP_P(( LDAP *ld, int msgid, int all, struct timeval *timeout,
29 LDAPMessage **result ));
30 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
31 static int try_read1msg LDAP_P(( LDAP *ld, int msgid, int all, Sockbuf *sb, LDAPConn *lc,
32 LDAPMessage **result ));
33 static unsigned long build_result_ber LDAP_P(( LDAP *ld, BerElement *ber, LDAPRequest *lr ));
34 static void merge_error_info LDAP_P(( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr ));
35 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
36 static int try_read1msg LDAP_P(( LDAP *ld, int msgid, int all, Sockbuf *sb,
37 LDAPMessage **result ));
38 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
39 #if defined( LDAP_CONNECTIONLESS ) || !defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS )
40 static int ldap_select1 LDAP_P(( LDAP *ld, struct timeval *timeout ));
45 * ldap_result - wait for an ldap result response to a message from the
46 * ldap server. If msgid is -1, any message will be accepted, otherwise
47 * ldap_result will wait for a response with msgid. If all is 0 the
48 * first message with id msgid will be accepted, otherwise, ldap_result
49 * will wait for all responses with id msgid and then return a pointer to
50 * the entire list of messages. This is only useful for search responses,
51 * which can be of two message types (zero or more entries, followed by an
52 * ldap result). The type of the first message received is returned.
53 * When waiting, any messages that have been abandoned are discarded.
56 * ldap_result( s, msgid, all, timeout, result )
59 ldap_result( LDAP *ld, int msgid, int all, struct timeval *timeout,
60 LDAPMessage **result )
62 LDAPMessage *lm, *lastlm, *nextlm;
65 * First, look through the list of responses we have received on
66 * this association and see if the response we're interested in
67 * is there. If it is, return it. If not, call wait4msg() to
68 * wait until it arrives or timeout occurs.
71 Debug( LDAP_DEBUG_TRACE, "ldap_result\n", 0, 0, 0 );
75 for ( lm = ld->ld_responses; lm != NULLMSG; lm = nextlm ) {
78 if ( ldap_abandoned( ld, lm->lm_msgid ) ) {
79 ldap_mark_abandoned( ld, lm->lm_msgid );
81 if ( lastlm == NULLMSG ) {
82 ld->ld_responses = lm->lm_next;
84 lastlm->lm_next = nextlm;
92 if ( msgid == LDAP_RES_ANY || lm->lm_msgid == msgid ) {
96 || (lm->lm_msgtype != LDAP_RES_SEARCH_RESULT
97 && lm->lm_msgtype != LDAP_RES_SEARCH_REFERENCE /* LDAPv3 */
98 && lm->lm_msgtype != LDAP_RES_SEARCH_ENTRY) )
101 for ( tmp = lm; tmp != NULLMSG; tmp = tmp->lm_chain ) {
102 if ( tmp->lm_msgtype == LDAP_RES_SEARCH_RESULT )
106 if ( tmp == NULLMSG ) {
107 return( wait4msg( ld, msgid, all, timeout,
115 if ( lm == NULLMSG ) {
116 return( wait4msg( ld, msgid, all, timeout, result ) );
119 if ( lastlm == NULLMSG ) {
120 ld->ld_responses = (all == 0 && lm->lm_chain != NULLMSG
121 ? lm->lm_chain : lm->lm_next);
123 lastlm->lm_next = (all == 0 && lm->lm_chain != NULLMSG
124 ? lm->lm_chain : lm->lm_next);
127 lm->lm_chain = NULLMSG;
128 lm->lm_next = NULLMSG;
131 ld->ld_errno = LDAP_SUCCESS;
132 return( lm->lm_msgtype );
136 wait4msg( LDAP *ld, int msgid, int all, struct timeval *timeout,
137 LDAPMessage **result )
140 struct timeval tv, *tvp;
141 time_t start_time = 0;
143 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
144 LDAPConn *lc, *nextlc;
145 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
148 if ( timeout == NULL ) {
149 Debug( LDAP_DEBUG_TRACE, "wait4msg (infinite timeout)\n",
152 Debug( LDAP_DEBUG_TRACE, "wait4msg (timeout %ld sec, %ld usec)\n",
153 (long) timeout->tv_sec, (long) timeout->tv_usec, 0 );
155 #endif /* LDAP_DEBUG */
157 if ( timeout == NULL ) {
162 start_time = time( NULL );
167 #ifndef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
169 if ( ! lber_pvt_sb_data_ready(&ld->ld_sb) ) {
170 rc = ldap_select1( ld, tvp );
172 if ( rc == 0 || ( rc == -1 && (
173 ( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART)
175 || errno != EINTR ))) {
176 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
183 rc = -2; /* select interrupted: loop */
185 rc = try_read1msg( ld, msgid, all, &ld->ld_sb, result );
187 #else /* !LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
189 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
190 ldap_dump_connection( ld, ld->ld_conns, 1 );
191 ldap_dump_requests_and_responses( ld );
193 #endif /* LDAP_DEBUG */
194 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
195 if ( lber_pvt_sb_data_ready(lc->lconn_sb) ) {
196 rc = try_read1msg( ld, msgid, all, lc->lconn_sb,
203 rc = do_ldap_select( ld, tvp );
208 Debug( LDAP_DEBUG_TRACE,
209 "do_ldap_select returned -1: errno %d\n",
214 if ( rc == 0 || ( rc == -1 && (
215 ( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART)
217 || errno != EINTR )))
219 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
225 rc = -2; /* select interrupted: loop */
228 for ( lc = ld->ld_conns; rc == -2 && lc != NULL;
230 nextlc = lc->lconn_next;
231 if ( lc->lconn_status ==
232 LDAP_CONNST_CONNECTED &&
233 ldap_is_read_ready( ld,
235 rc = try_read1msg( ld, msgid, all,
236 lc->lconn_sb, lc, result );
241 #endif /* !LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
243 if ( rc == -2 && tvp != NULL ) {
244 tmp_time = time( NULL );
245 if (( tv.tv_sec -= ( tmp_time - start_time )) <= 0 ) {
246 rc = 0; /* timed out */
247 ld->ld_errno = LDAP_TIMEOUT;
251 Debug( LDAP_DEBUG_TRACE, "wait4msg: %ld secs to go\n",
252 (long) tv.tv_sec, 0, 0 );
253 start_time = tmp_time;
262 try_read1msg( LDAP *ld, int msgid, int all, Sockbuf *sb,
263 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
265 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
266 LDAPMessage **result )
269 LDAPMessage *new, *l, *prev, *tmp;
271 unsigned long tag, len;
273 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
276 int rc, refer_cnt, hadref, simple_request;
279 ber = &lc->lconn_ber;
282 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
284 Debug( LDAP_DEBUG_TRACE, "read1msg\n", 0, 0, 0 );
286 ber_init_w_nullc( &ber, 0 );
287 ldap_set_ber_options( ld, &ber );
289 /* get the next message */
290 if ( (tag = ber_get_next( sb, &len, ber ))
291 != LDAP_TAG_MESSAGE ) {
292 if ( tag == LBER_DEFAULT) {
294 Debug( LDAP_DEBUG_CONNS,
295 "ber_get_next failed.\n", 0, 0, 0 );
298 if (errno==EWOULDBLOCK) return -2;
301 if (errno == EAGAIN) return -2;
303 ld->ld_errno = LDAP_SERVER_DOWN;
306 ld->ld_errno = LDAP_LOCAL_ERROR;
311 if ( ber_get_int( ber, &id ) == LBER_ERROR ) {
312 ld->ld_errno = LDAP_DECODING_ERROR;
316 /* if it's been abandoned, toss it */
317 if ( ldap_abandoned( ld, (int)id ) ) {
318 ber_clear( ber, 1 ); /* gack! */
319 return( -2 ); /* continue looking */
322 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
323 if (( lr = ldap_find_request_by_msgid( ld, id )) == NULL ) {
324 Debug( LDAP_DEBUG_ANY,
325 "no request for response with msgid %ld (tossing)\n",
327 ber_clear( ber, 1 ); /* gack! */
328 return( -2 ); /* continue looking */
330 Debug( LDAP_DEBUG_TRACE, "got %s msgid %ld, original id %d\n",
331 ( tag == LDAP_RES_SEARCH_ENTRY ) ? "entry" : "result", id,
334 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
336 /* the message type */
337 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
338 ld->ld_errno = LDAP_DECODING_ERROR;
342 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
344 hadref = simple_request = 0;
345 rc = -2; /* default is to keep looking (no response found) */
346 lr->lr_res_msgtype = tag;
348 if ( tag != LDAP_RES_SEARCH_ENTRY ) {
349 if ( ld->ld_version >= LDAP_VERSION2 &&
350 ( lr->lr_parent != NULL ||
351 ( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS)
352 != LDAP_OPT_OFF ) ) )
354 tmpber = *ber; /* struct copy */
355 if ( ber_scanf( &tmpber, "{iaa}", &lderr,
356 &lr->lr_res_matched, &lr->lr_res_error )
358 if ( lderr != LDAP_SUCCESS ) {
359 /* referrals are in error string */
360 refer_cnt = ldap_chase_referrals( ld, lr,
361 &lr->lr_res_error, &hadref );
364 /* save errno, message, and matched string */
365 if ( !hadref || lr->lr_res_error == NULL ) {
366 lr->lr_res_errno = ( lderr ==
367 LDAP_PARTIAL_RESULTS ) ? LDAP_SUCCESS
369 } else if ( ld->ld_errno != LDAP_SUCCESS ) {
370 lr->lr_res_errno = ld->ld_errno;
372 lr->lr_res_errno = LDAP_PARTIAL_RESULTS;
374 Debug( LDAP_DEBUG_TRACE,
375 "new result: res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
376 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
377 lr->lr_res_matched ? lr->lr_res_matched : "" );
381 Debug( LDAP_DEBUG_TRACE,
382 "read1msg: %d new referrals\n", refer_cnt, 0, 0 );
384 if ( refer_cnt != 0 ) { /* chasing referrals */
385 ber_clear( ber, 1 ); /* gack! */
386 if ( refer_cnt < 0 ) {
387 return( -1 ); /* fatal error */
389 lr->lr_status = LDAP_REQST_CHASINGREFS;
391 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
392 /* request without any referrals */
393 simple_request = ( hadref ? 0 : 1 );
395 /* request with referrals or child request */
396 ber_clear( ber, 1 ); /* gack! */
399 while ( lr->lr_parent != NULL ) {
400 merge_error_info( ld, lr->lr_parent, lr );
403 if ( --lr->lr_outrefcnt > 0 ) {
404 break; /* not completely done yet */
408 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
410 tag = lr->lr_res_msgtype;
411 Debug( LDAP_DEBUG_ANY, "request %ld done\n",
413 Debug( LDAP_DEBUG_TRACE,
414 "res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
415 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
416 lr->lr_res_matched ? lr->lr_res_matched : "" );
417 if ( !simple_request ) {
418 ber_clear( ber, 1 ); /* gack! */
419 if ( build_result_ber( ld, ber, lr )
421 ld->ld_errno = LDAP_NO_MEMORY;
422 rc = -1; /* fatal error */
426 ldap_free_request( ld, lr );
430 ldap_free_connection( ld, lc, 0, 1 );
435 if ( ber->ber_buf == NULL ) {
439 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
440 /* make a new ldap message */
441 if ( (new = (LDAPMessage *) calloc( 1, sizeof(LDAPMessage) ))
443 ld->ld_errno = LDAP_NO_MEMORY;
446 new->lm_msgid = (int)id;
447 new->lm_msgtype = tag;
448 new->lm_ber = ber_dup( ber );
449 ber_clear( ber, 0 ); /* don't kill buffer */
452 if ( ld->ld_cache != NULL ) {
453 ldap_add_result_to_cache( ld, new );
455 #endif /* LDAP_NOCACHE */
457 /* is this the one we're looking for? */
458 if ( msgid == LDAP_RES_ANY || id == msgid ) {
460 || (new->lm_msgtype != LDAP_RES_SEARCH_RESULT
461 && new->lm_msgtype != LDAP_RES_SEARCH_ENTRY) ) {
463 ld->ld_errno = LDAP_SUCCESS;
465 } else if ( new->lm_msgtype == LDAP_RES_SEARCH_RESULT) {
466 foundit = 1; /* return the chain later */
471 * if not, we must add it to the list of responses. if
472 * the msgid is already there, it must be part of an existing
477 for ( l = ld->ld_responses; l != NULLMSG; l = l->lm_next ) {
478 if ( l->lm_msgid == new->lm_msgid )
483 /* not part of an existing search response */
484 if ( l == NULLMSG ) {
487 ld->ld_errno = LDAP_SUCCESS;
491 new->lm_next = ld->ld_responses;
492 ld->ld_responses = new;
493 return( -2 ); /* continue looking */
496 Debug( LDAP_DEBUG_TRACE, "adding response id %d type %d:\n",
497 new->lm_msgid, new->lm_msgtype, 0 );
499 /* part of a search response - add to end of list of entries */
500 for ( tmp = l; tmp->lm_chain != NULLMSG &&
501 tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY;
502 tmp = tmp->lm_chain )
506 /* return the whole chain if that's what we were looking for */
508 if ( prev == NULLMSG )
509 ld->ld_responses = l->lm_next;
511 prev->lm_next = l->lm_next;
513 ld->ld_errno = LDAP_SUCCESS;
514 #ifdef LDAP_WORLD_P16
516 * XXX questionable fix; see text for [P16] on
517 * http://www.critical-angle.com/ldapworld/patch/
519 * inclusion of this patch causes searchs to hang on
522 return( l->lm_msgtype );
523 #else /* LDAP_WORLD_P16 */
525 #endif /* !LDAP_WORLD_P16 */
528 return( -2 ); /* continue looking */
532 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
534 build_result_ber( LDAP *ld, BerElement *ber, LDAPRequest *lr )
539 ber_init_w_nullc( ber, 0 );
540 ldap_set_ber_options( ld, ber );
541 if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
542 (long)lr->lr_res_msgtype, lr->lr_res_errno,
543 lr->lr_res_matched ? lr->lr_res_matched : "",
544 lr->lr_res_error ? lr->lr_res_error : "" ) == -1 ) {
545 return( LBER_ERROR );
549 if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) {
550 return( LBER_ERROR );
553 if ( ber_get_int( ber, &along ) == LBER_ERROR ) {
554 return( LBER_ERROR );
557 return( ber_peek_tag( ber, &len ));
562 merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
565 * Merge error information in "lr" with "parentr" error code and string.
567 if ( lr->lr_res_errno == LDAP_PARTIAL_RESULTS ) {
568 parentr->lr_res_errno = lr->lr_res_errno;
569 if ( lr->lr_res_error != NULL ) {
570 (void)ldap_append_referral( ld, &parentr->lr_res_error,
573 } else if ( lr->lr_res_errno != LDAP_SUCCESS &&
574 parentr->lr_res_errno == LDAP_SUCCESS ) {
575 parentr->lr_res_errno = lr->lr_res_errno;
576 if ( parentr->lr_res_error != NULL ) {
577 free( parentr->lr_res_error );
579 parentr->lr_res_error = lr->lr_res_error;
580 lr->lr_res_error = NULL;
581 if ( NAME_ERROR( lr->lr_res_errno )) {
582 if ( parentr->lr_res_matched != NULL ) {
583 free( parentr->lr_res_matched );
585 parentr->lr_res_matched = lr->lr_res_matched;
586 lr->lr_res_matched = NULL;
590 Debug( LDAP_DEBUG_TRACE, "merged parent (id %d) error info: ",
591 parentr->lr_msgid, 0, 0 );
592 Debug( LDAP_DEBUG_TRACE, "result errno %d, error <%s>, matched <%s>\n",
593 parentr->lr_res_errno, parentr->lr_res_error ?
594 parentr->lr_res_error : "", parentr->lr_res_matched ?
595 parentr->lr_res_matched : "" );
597 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
601 #if defined( LDAP_CONNECTIONLESS ) || !defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS )
604 ldap_select1( LDAP *ld, struct timeval *timeout )
609 if ( tblsize == 0 ) {
611 tblsize = sysconf( _SC_OPEN_MAX );
612 #elif HAVE_GETDTABLESIZE
613 tblsize = getdtablesize();
615 tblsize = FD_SETSIZE;
618 if ( tblsize > FD_SETSIZE ) {
619 tblsize = FD_SETSIZE;
621 #endif /* FD_SETSIZE */
625 FD_SET( lber_pvt_sb_get_desc(&ld->ld_sb), &readfds );
627 return( select( tblsize, &readfds, 0, 0, timeout ) );
630 #endif /* !LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
634 ldap_msgtype( LDAPMessage *lm )
636 return( lm ? lm->lm_msgtype : -1 );
640 ldap_msgid( LDAPMessage *lm )
642 return( lm ? lm->lm_msgid : -1 );
647 ldap_msgfree( LDAPMessage *lm )
652 Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
654 for ( ; lm != NULLMSG; lm = next ) {
656 type = lm->lm_msgtype;
657 ber_free( lm->lm_ber, 1 );
665 * ldap_msgdelete - delete a message. It returns:
666 * 0 if the entire message was deleted
667 * -1 if the message was not found, or only part of it was found
670 ldap_msgdelete( LDAP *ld, int msgid )
672 LDAPMessage *lm, *prev;
674 Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete\n", 0, 0, 0 );
677 for ( lm = ld->ld_responses; lm != NULLMSG; lm = lm->lm_next ) {
678 if ( lm->lm_msgid == msgid )
686 if ( prev == NULLMSG )
687 ld->ld_responses = lm->lm_next;
689 prev->lm_next = lm->lm_next;
691 if ( ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY )
699 * return 1 if message msgid is waiting to be abandoned, 0 otherwise
702 ldap_abandoned( LDAP *ld, int msgid )
706 if ( ld->ld_abandoned == NULL )
709 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
710 if ( ld->ld_abandoned[i] == msgid )
718 ldap_mark_abandoned( LDAP *ld, int msgid )
722 if ( ld->ld_abandoned == NULL )
725 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
726 if ( ld->ld_abandoned[i] == msgid )
729 if ( ld->ld_abandoned[i] == -1 )
732 for ( ; ld->ld_abandoned[i] != -1; i++ ) {
733 ld->ld_abandoned[i] = ld->ld_abandoned[i + 1];
740 #ifdef LDAP_CONNECTIONLESS
742 cldap_getmsg( LDAP *ld, struct timeval *timeout, BerElement *ber )
745 unsigned long tag, len;
747 if ( ! lber_pvt_sb_data_ready(&ld->ld_sb) ) {
748 rc = ldap_select1( ld, timeout );
749 if ( rc == -1 || rc == 0 ) {
750 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
756 /* get the next message */
757 if ( (tag = ber_get_next( &ld->ld_sb, &len, ber ))
758 != LDAP_TAG_MESSAGE ) {
759 ld->ld_errno = (tag == LBER_DEFAULT ? LDAP_SERVER_DOWN :
766 #endif /* LDAP_CONNECTIONLESS */