3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1990 Regents of the University of Michigan.
10 * result.c - wait for an ldap result
15 * LDAPResult ::= SEQUENCE {
16 * resultCode ENUMERATED { ... },
18 * errorMessage LDAPString,
19 * referral Referral OPTIONAL
21 * Referral ::= SEQUENCE OF LDAPURL (one or more)
22 * LDAPURL ::= LDAPString (limited to URL chars)
29 #include <ac/stdlib.h>
32 #include <ac/socket.h>
33 #include <ac/string.h>
35 #include <ac/unistd.h>
40 static int ldap_abandoned LDAP_P(( LDAP *ld, ber_int_t msgid ));
41 static int ldap_mark_abandoned LDAP_P(( LDAP *ld, ber_int_t msgid ));
42 static int wait4msg LDAP_P(( LDAP *ld, ber_int_t msgid, int all, struct timeval *timeout,
43 LDAPMessage **result ));
44 static ber_tag_t try_read1msg LDAP_P(( LDAP *ld, ber_int_t msgid,
45 int all, Sockbuf *sb, LDAPConn *lc, LDAPMessage **result ));
46 static ber_tag_t build_result_ber LDAP_P(( LDAP *ld, BerElement **bp, LDAPRequest *lr ));
47 static void merge_error_info LDAP_P(( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr ));
51 * ldap_result - wait for an ldap result response to a message from the
52 * ldap server. If msgid is -1, any message will be accepted, otherwise
53 * ldap_result will wait for a response with msgid. If all is 0 the
54 * first message with id msgid will be accepted, otherwise, ldap_result
55 * will wait for all responses with id msgid and then return a pointer to
56 * the entire list of messages. This is only useful for search responses,
57 * which can be of two message types (zero or more entries, followed by an
58 * ldap result). The type of the first message received is returned.
59 * When waiting, any messages that have been abandoned are discarded.
62 * ldap_result( s, msgid, all, timeout, result )
69 struct timeval *timeout,
70 LDAPMessage **result )
72 LDAPMessage *lm, *lastlm, *nextlm;
75 assert( result != NULL );
77 Debug( LDAP_DEBUG_TRACE, "ldap_result\n", 0, 0, 0 );
83 if( result == NULL ) {
84 ld->ld_errno = LDAP_PARAM_ERROR;
89 * First, look through the list of responses we have received on
90 * this association and see if the response we're interested in
91 * is there. If it is, return it. If not, call wait4msg() to
92 * wait until it arrives or timeout occurs.
97 for ( lm = ld->ld_responses; lm != NULL; lm = nextlm ) {
100 if ( ldap_abandoned( ld, lm->lm_msgid ) ) {
101 ldap_mark_abandoned( ld, lm->lm_msgid );
103 if ( lastlm == NULL ) {
104 ld->ld_responses = lm->lm_next;
106 lastlm->lm_next = nextlm;
114 if ( msgid == LDAP_RES_ANY || lm->lm_msgid == msgid ) {
118 || (lm->lm_msgtype != LDAP_RES_SEARCH_RESULT
119 && lm->lm_msgtype != LDAP_RES_SEARCH_REFERENCE /* LDAPv3 */
120 && lm->lm_msgtype != LDAP_RES_SEARCH_ENTRY) )
123 for ( tmp = lm; tmp != NULL; tmp = tmp->lm_chain ) {
124 if ( tmp->lm_msgtype == LDAP_RES_SEARCH_RESULT )
137 return( wait4msg( ld, msgid, all, timeout, result ) );
140 if ( lastlm == NULL ) {
141 ld->ld_responses = (all == 0 && lm->lm_chain != NULL
142 ? lm->lm_chain : lm->lm_next);
144 lastlm->lm_next = (all == 0 && lm->lm_chain != NULL
145 ? lm->lm_chain : lm->lm_next);
152 ld->ld_errno = LDAP_SUCCESS;
153 return( lm->lm_msgtype );
161 struct timeval *timeout,
162 LDAPMessage **result )
165 struct timeval tv, *tvp;
166 time_t start_time = 0;
168 LDAPConn *lc, *nextlc;
170 assert( ld != NULL );
171 assert( result != NULL );
174 if ( timeout == NULL ) {
175 Debug( LDAP_DEBUG_TRACE, "wait4msg (infinite timeout)\n",
178 Debug( LDAP_DEBUG_TRACE, "wait4msg (timeout %ld sec, %ld usec)\n",
179 (long) timeout->tv_sec, (long) timeout->tv_usec, 0 );
181 #endif /* LDAP_DEBUG */
183 if ( timeout == NULL ) {
188 start_time = time( NULL );
194 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
195 ldap_dump_connection( ld, ld->ld_conns, 1 );
196 ldap_dump_requests_and_responses( ld );
198 #endif /* LDAP_DEBUG */
199 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
200 if ( ber_pvt_sb_data_ready(lc->lconn_sb) ) {
201 rc = try_read1msg( ld, msgid, all, lc->lconn_sb,
208 rc = do_ldap_select( ld, tvp );
213 Debug( LDAP_DEBUG_TRACE,
214 "do_ldap_select returned -1: errno %d\n",
219 if ( rc == 0 || ( rc == -1 && (
220 !LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART)
221 || errno != EINTR )))
223 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
229 rc = -2; /* select interrupted: loop */
232 for ( lc = ld->ld_conns; rc == -2 && lc != NULL;
234 nextlc = lc->lconn_next;
235 if ( lc->lconn_status ==
236 LDAP_CONNST_CONNECTED &&
237 ldap_is_read_ready( ld,
239 rc = try_read1msg( ld, msgid, all,
240 lc->lconn_sb, lc, result );
246 if ( rc == -2 && tvp != NULL ) {
247 tmp_time = time( NULL );
248 if (( tv.tv_sec -= ( tmp_time - start_time )) <= 0 ) {
249 rc = 0; /* timed out */
250 ld->ld_errno = LDAP_TIMEOUT;
254 Debug( LDAP_DEBUG_TRACE, "wait4msg: %ld secs to go\n",
255 (long) tv.tv_sec, 0, 0 );
256 start_time = tmp_time;
271 LDAPMessage **result )
274 LDAPMessage *new, *l, *prev, *tmp;
281 int rc, refer_cnt, hadref, simple_request;
284 assert( ld != NULL );
285 assert( lc != NULL );
287 Debug( LDAP_DEBUG_TRACE, "read1msg\n", 0, 0, 0 );
289 if ( lc->lconn_ber == NULL ) {
290 lc->lconn_ber = ldap_alloc_ber_with_options(ld);
292 if( lc->lconn_ber == NULL ) {
298 assert( BER_VALID (ber) );
300 /* get the next message */
302 if ( (tag = ber_get_next( sb, &len, ber ))
303 != LDAP_TAG_MESSAGE ) {
304 if ( tag == LBER_DEFAULT) {
306 Debug( LDAP_DEBUG_CONNS,
307 "ber_get_next failed.\n", 0, 0, 0 );
310 if (errno==EWOULDBLOCK) return -2;
313 if (errno == EAGAIN) return -2;
315 ld->ld_errno = LDAP_SERVER_DOWN;
318 ld->ld_errno = LDAP_LOCAL_ERROR;
323 * We read a complete message.
324 * The connection should no longer need this ber.
326 lc->lconn_ber = NULL;
329 if ( ber_get_int( ber, &id ) == LBER_ERROR ) {
331 ld->ld_errno = LDAP_DECODING_ERROR;
335 /* if it's been abandoned, toss it */
336 if ( ldap_abandoned( ld, id ) ) {
338 return( -2 ); /* continue looking */
341 if (( lr = ldap_find_request_by_msgid( ld, id )) == NULL ) {
342 Debug( LDAP_DEBUG_ANY,
343 "no request for response with msgid %ld (tossing)\n",
346 return( -2 ); /* continue looking */
349 /* the message type */
350 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
351 ld->ld_errno = LDAP_DECODING_ERROR;
356 Debug( LDAP_DEBUG_TRACE, "ldap_read: %s msgid %ld, original id %ld\n",
357 ( tag == LDAP_RES_SEARCH_ENTRY ) ? "entry" :
358 ( tag == LDAP_RES_SEARCH_REFERENCE ) ? "reference" : "result",
359 (long) id, (long) lr->lr_origid );
363 hadref = simple_request = 0;
364 rc = -2; /* default is to keep looking (no response found) */
365 lr->lr_res_msgtype = tag;
367 if ( tag != LDAP_RES_SEARCH_ENTRY ) {
368 if ( ld->ld_version >= LDAP_VERSION2 &&
369 ( lr->lr_parent != NULL ||
370 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ) )
372 tmpber = *ber; /* struct copy */
373 if ( ber_scanf( &tmpber, "{iaa}", &lderr,
374 &lr->lr_res_matched, &lr->lr_res_error )
376 if ( lderr != LDAP_SUCCESS ) {
377 /* referrals are in error string */
378 refer_cnt = ldap_chase_referrals( ld, lr,
379 &lr->lr_res_error, &hadref );
382 /* save errno, message, and matched string */
383 if ( !hadref || lr->lr_res_error == NULL ) {
384 lr->lr_res_errno = ( lderr ==
385 LDAP_PARTIAL_RESULTS ) ? LDAP_SUCCESS
387 } else if ( ld->ld_errno != LDAP_SUCCESS ) {
388 lr->lr_res_errno = ld->ld_errno;
390 lr->lr_res_errno = LDAP_PARTIAL_RESULTS;
392 Debug( LDAP_DEBUG_TRACE,
393 "new result: res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
394 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
395 lr->lr_res_matched ? lr->lr_res_matched : "" );
399 Debug( LDAP_DEBUG_TRACE,
400 "read1msg: %d new referrals\n", refer_cnt, 0, 0 );
402 if ( refer_cnt != 0 ) { /* chasing referrals */
405 if ( refer_cnt < 0 ) {
406 return( -1 ); /* fatal error */
408 lr->lr_status = LDAP_REQST_CHASINGREFS;
410 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
411 /* request without any referrals */
412 simple_request = ( hadref ? 0 : 1 );
414 /* request with referrals or child request */
419 while ( lr->lr_parent != NULL ) {
420 merge_error_info( ld, lr->lr_parent, lr );
423 if ( --lr->lr_outrefcnt > 0 ) {
424 break; /* not completely done yet */
428 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
430 tag = lr->lr_res_msgtype;
431 Debug( LDAP_DEBUG_ANY, "request %ld done\n",
433 Debug( LDAP_DEBUG_TRACE,
434 "res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
435 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
436 lr->lr_res_matched ? lr->lr_res_matched : "" );
437 if ( !simple_request ) {
440 if ( build_result_ber( ld, &ber, lr )
442 rc = -1; /* fatal error */
446 ldap_free_request( ld, lr );
450 ldap_free_connection( ld, lc, 0, 1 );
459 /* make a new ldap message */
460 if ( (new = (LDAPMessage *) LDAP_CALLOC( 1, sizeof(LDAPMessage) ))
462 ld->ld_errno = LDAP_NO_MEMORY;
465 new->lm_msgid = (int)id;
466 new->lm_msgtype = tag;
470 if ( ld->ld_cache != NULL ) {
471 ldap_add_result_to_cache( ld, new );
473 #endif /* LDAP_NOCACHE */
475 /* is this the one we're looking for? */
476 if ( msgid == LDAP_RES_ANY || id == msgid ) {
478 || (new->lm_msgtype != LDAP_RES_SEARCH_RESULT
479 && new->lm_msgtype != LDAP_RES_SEARCH_ENTRY) ) {
481 ld->ld_errno = LDAP_SUCCESS;
483 } else if ( new->lm_msgtype == LDAP_RES_SEARCH_RESULT) {
484 foundit = 1; /* return the chain later */
489 * if not, we must add it to the list of responses. if
490 * the msgid is already there, it must be part of an existing
495 for ( l = ld->ld_responses; l != NULL; l = l->lm_next ) {
496 if ( l->lm_msgid == new->lm_msgid )
501 /* not part of an existing search response */
505 ld->ld_errno = LDAP_SUCCESS;
509 new->lm_next = ld->ld_responses;
510 ld->ld_responses = new;
511 return( -2 ); /* continue looking */
514 Debug( LDAP_DEBUG_TRACE, "adding response id %ld type %ld:\n",
515 (long) new->lm_msgid, (long) new->lm_msgtype, 0 );
517 /* part of a search response - add to end of list of entries */
518 for ( tmp = l; tmp->lm_chain != NULL &&
519 tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY;
520 tmp = tmp->lm_chain )
524 /* return the whole chain if that's what we were looking for */
527 ld->ld_responses = l->lm_next;
529 prev->lm_next = l->lm_next;
531 ld->ld_errno = LDAP_SUCCESS;
532 #ifdef LDAP_WORLD_P16
534 * XXX questionable fix; see text for [P16] on
535 * http://www.critical-angle.com/ldapworld/patch/
537 * inclusion of this patch causes searchs to hang on
540 return( l->lm_msgtype );
541 #else /* LDAP_WORLD_P16 */
543 #endif /* !LDAP_WORLD_P16 */
546 return( -2 ); /* continue looking */
551 build_result_ber( LDAP *ld, BerElement **bp, LDAPRequest *lr )
559 ber = ldap_alloc_ber_with_options( ld );
562 ld->ld_errno = LDAP_NO_MEMORY;
566 if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
567 lr->lr_res_msgtype, lr->lr_res_errno,
568 lr->lr_res_matched ? lr->lr_res_matched : "",
569 lr->lr_res_error ? lr->lr_res_error : "" ) == -1 ) {
571 ld->ld_errno = LDAP_ENCODING_ERROR;
573 return( LBER_ERROR );
578 if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) {
579 ld->ld_errno = LDAP_DECODING_ERROR;
581 return( LBER_ERROR );
584 if ( ber_get_int( ber, &along ) == LBER_ERROR ) {
585 ld->ld_errno = LDAP_DECODING_ERROR;
587 return( LBER_ERROR );
590 tag = ber_peek_tag( ber, &len );
592 if ( tag == LBER_ERROR ) {
593 ld->ld_errno = LDAP_DECODING_ERROR;
595 return( LBER_ERROR );
604 merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
607 * Merge error information in "lr" with "parentr" error code and string.
609 if ( lr->lr_res_errno == LDAP_PARTIAL_RESULTS ) {
610 parentr->lr_res_errno = lr->lr_res_errno;
611 if ( lr->lr_res_error != NULL ) {
612 (void)ldap_append_referral( ld, &parentr->lr_res_error,
615 } else if ( lr->lr_res_errno != LDAP_SUCCESS &&
616 parentr->lr_res_errno == LDAP_SUCCESS ) {
617 parentr->lr_res_errno = lr->lr_res_errno;
618 if ( parentr->lr_res_error != NULL ) {
619 LDAP_FREE( parentr->lr_res_error );
621 parentr->lr_res_error = lr->lr_res_error;
622 lr->lr_res_error = NULL;
623 if ( LDAP_NAME_ERROR( lr->lr_res_errno )) {
624 if ( parentr->lr_res_matched != NULL ) {
625 LDAP_FREE( parentr->lr_res_matched );
627 parentr->lr_res_matched = lr->lr_res_matched;
628 lr->lr_res_matched = NULL;
632 Debug( LDAP_DEBUG_TRACE, "merged parent (id %d) error info: ",
633 parentr->lr_msgid, 0, 0 );
634 Debug( LDAP_DEBUG_TRACE, "result errno %d, error <%s>, matched <%s>\n",
635 parentr->lr_res_errno, parentr->lr_res_error ?
636 parentr->lr_res_error : "", parentr->lr_res_matched ?
637 parentr->lr_res_matched : "" );
643 ldap_msgtype( LDAPMessage *lm )
645 assert( lm != NULL );
646 return ( lm != NULL ) ? lm->lm_msgtype : -1;
651 ldap_msgid( LDAPMessage *lm )
653 assert( lm != NULL );
655 return ( lm != NULL ) ? lm->lm_msgid : -1;
660 ldap_msgfree( LDAPMessage *lm )
665 assert( lm != NULL );
667 Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
669 for ( ; lm != NULL; lm = next ) {
671 type = lm->lm_msgtype;
672 ber_free( lm->lm_ber, 1 );
673 LDAP_FREE( (char *) lm );
680 * ldap_msgdelete - delete a message. It returns:
681 * 0 if the entire message was deleted
682 * -1 if the message was not found, or only part of it was found
685 ldap_msgdelete( LDAP *ld, int msgid )
687 LDAPMessage *lm, *prev;
689 assert( ld != NULL );
691 Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete\n", 0, 0, 0 );
694 for ( lm = ld->ld_responses; lm != NULL; lm = lm->lm_next ) {
695 if ( lm->lm_msgid == msgid )
704 ld->ld_responses = lm->lm_next;
706 prev->lm_next = lm->lm_next;
708 if ( ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY )
716 * return 1 if message msgid is waiting to be abandoned, 0 otherwise
719 ldap_abandoned( LDAP *ld, ber_int_t msgid )
723 if ( ld->ld_abandoned == NULL )
726 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
727 if ( ld->ld_abandoned[i] == msgid )
735 ldap_mark_abandoned( LDAP *ld, ber_int_t msgid )
739 if ( ld->ld_abandoned == NULL )
742 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
743 if ( ld->ld_abandoned[i] == msgid )
746 if ( ld->ld_abandoned[i] == -1 )
749 for ( ; ld->ld_abandoned[i] != -1; i++ ) {
750 ld->ld_abandoned[i] = ld->ld_abandoned[i + 1];
757 #ifdef LDAP_CONNECTIONLESS
759 cldap_getmsg( LDAP *ld, struct timeval *timeout, BerElement *ber )
765 if ( ! ber_pvt_sb_data_ready(&ld->ld_sb) ) {
766 /* restored from ldap_select1() in result.c version 1.24 */
768 if ( ldap_int_tblsize == 0 )
771 FD_SET( ber_pvt_sb_get_desc(&ld->ld_sb), &readfds );
772 rc = select( ldap_int_tblsize, &readfds, 0, 0, timeout );
774 if ( rc == -1 || rc == 0 ) {
775 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
781 /* get the next message */
782 if ( (tag = ber_get_next( &ld->ld_sb, &len, ber ))
783 != LDAP_TAG_MESSAGE ) {
784 ld->ld_errno = (tag == LBER_DEFAULT ? LDAP_SERVER_DOWN :
791 #endif /* LDAP_CONNECTIONLESS */