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
14 * LDAPResult ::= SEQUENCE {
15 * resultCode ENUMERATED { ... },
17 * errorMessage LDAPString,
18 * referral Referral OPTIONAL
20 * Referral ::= SEQUENCE OF LDAPURL (one or more)
21 * LDAPURL ::= LDAPString (limited to URL chars)
28 #include <ac/stdlib.h>
31 #include <ac/socket.h>
32 #include <ac/string.h>
34 #include <ac/unistd.h>
39 static int ldap_abandoned LDAP_P(( LDAP *ld, ber_int_t msgid ));
40 static int ldap_mark_abandoned LDAP_P(( LDAP *ld, ber_int_t msgid ));
41 static int wait4msg LDAP_P(( LDAP *ld, ber_int_t msgid, int all, struct timeval *timeout,
42 LDAPMessage **result ));
43 static ber_tag_t try_read1msg LDAP_P(( LDAP *ld, ber_int_t msgid,
44 int all, Sockbuf *sb, LDAPConn *lc, LDAPMessage **result ));
45 static ber_tag_t build_result_ber LDAP_P(( LDAP *ld, BerElement **bp, LDAPRequest *lr ));
46 static void merge_error_info LDAP_P(( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr ));
50 * ldap_result - wait for an ldap result response to a message from the
51 * ldap server. If msgid is -1, any message will be accepted, otherwise
52 * ldap_result will wait for a response with msgid. If all is 0 the
53 * first message with id msgid will be accepted, otherwise, ldap_result
54 * will wait for all responses with id msgid and then return a pointer to
55 * the entire list of messages. This is only useful for search responses,
56 * which can be of two message types (zero or more entries, followed by an
57 * ldap result). The type of the first message received is returned.
58 * When waiting, any messages that have been abandoned are discarded.
61 * ldap_result( s, msgid, all, timeout, result )
64 ldap_result( LDAP *ld, int msgid, int all, struct timeval *timeout,
65 LDAPMessage **result )
67 LDAPMessage *lm, *lastlm, *nextlm;
70 assert( result != NULL );
72 Debug( LDAP_DEBUG_TRACE, "ldap_result\n", 0, 0, 0 );
78 if( result == NULL ) {
79 ld->ld_errno = LDAP_PARAM_ERROR;
84 * First, look through the list of responses we have received on
85 * this association and see if the response we're interested in
86 * is there. If it is, return it. If not, call wait4msg() to
87 * wait until it arrives or timeout occurs.
92 for ( lm = ld->ld_responses; lm != NULL; lm = nextlm ) {
95 if ( ldap_abandoned( ld, lm->lm_msgid ) ) {
96 ldap_mark_abandoned( ld, lm->lm_msgid );
98 if ( lastlm == NULL ) {
99 ld->ld_responses = lm->lm_next;
101 lastlm->lm_next = nextlm;
109 if ( msgid == LDAP_RES_ANY || lm->lm_msgid == msgid ) {
113 || (lm->lm_msgtype != LDAP_RES_SEARCH_RESULT
114 && lm->lm_msgtype != LDAP_RES_SEARCH_REFERENCE /* LDAPv3 */
115 && lm->lm_msgtype != LDAP_RES_SEARCH_ENTRY) )
118 for ( tmp = lm; tmp != NULL; tmp = tmp->lm_chain ) {
119 if ( tmp->lm_msgtype == LDAP_RES_SEARCH_RESULT )
124 return( wait4msg( ld, msgid, all, timeout,
133 return( wait4msg( ld, msgid, all, timeout, result ) );
136 if ( lastlm == NULL ) {
137 ld->ld_responses = (all == 0 && lm->lm_chain != NULL
138 ? lm->lm_chain : lm->lm_next);
140 lastlm->lm_next = (all == 0 && lm->lm_chain != NULL
141 ? lm->lm_chain : lm->lm_next);
148 ld->ld_errno = LDAP_SUCCESS;
149 return( lm->lm_msgtype );
157 struct timeval *timeout,
158 LDAPMessage **result )
161 struct timeval tv, *tvp;
162 time_t start_time = 0;
164 LDAPConn *lc, *nextlc;
166 assert( ld != NULL );
167 assert( result != NULL );
170 if ( timeout == NULL ) {
171 Debug( LDAP_DEBUG_TRACE, "wait4msg (infinite timeout)\n",
174 Debug( LDAP_DEBUG_TRACE, "wait4msg (timeout %ld sec, %ld usec)\n",
175 (long) timeout->tv_sec, (long) timeout->tv_usec, 0 );
177 #endif /* LDAP_DEBUG */
179 if ( timeout == NULL ) {
184 start_time = time( NULL );
190 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
191 ldap_dump_connection( ld, ld->ld_conns, 1 );
192 ldap_dump_requests_and_responses( ld );
194 #endif /* LDAP_DEBUG */
195 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
196 if ( ber_pvt_sb_data_ready(lc->lconn_sb) ) {
197 rc = try_read1msg( ld, msgid, all, lc->lconn_sb,
204 rc = do_ldap_select( ld, tvp );
209 Debug( LDAP_DEBUG_TRACE,
210 "do_ldap_select returned -1: errno %d\n",
215 if ( rc == 0 || ( rc == -1 && (
216 !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 );
242 if ( rc == -2 && tvp != NULL ) {
243 tmp_time = time( NULL );
244 if (( tv.tv_sec -= ( tmp_time - start_time )) <= 0 ) {
245 rc = 0; /* timed out */
246 ld->ld_errno = LDAP_TIMEOUT;
250 Debug( LDAP_DEBUG_TRACE, "wait4msg: %ld secs to go\n",
251 (long) tv.tv_sec, 0, 0 );
252 start_time = tmp_time;
267 LDAPMessage **result )
270 LDAPMessage *new, *l, *prev, *tmp;
277 int rc, refer_cnt, hadref, simple_request;
280 assert( ld != NULL );
281 assert( lc != NULL );
283 Debug( LDAP_DEBUG_TRACE, "read1msg\n", 0, 0, 0 );
285 if ( lc->lconn_ber == NULL ) {
286 lc->lconn_ber = ldap_alloc_ber_with_options(ld);
288 if( lc->lconn_ber == NULL ) {
294 assert( BER_VALID (ber) );
296 /* get the next message */
298 if ( (tag = ber_get_next( sb, &len, ber ))
299 != LDAP_TAG_MESSAGE ) {
300 if ( tag == LBER_DEFAULT) {
302 Debug( LDAP_DEBUG_CONNS,
303 "ber_get_next failed.\n", 0, 0, 0 );
306 if (errno==EWOULDBLOCK) return -2;
309 if (errno == EAGAIN) return -2;
311 ld->ld_errno = LDAP_SERVER_DOWN;
314 ld->ld_errno = LDAP_LOCAL_ERROR;
319 * We read a complete message.
320 * The connection should no longer need this ber.
322 lc->lconn_ber = NULL;
325 if ( ber_get_int( ber, &id ) == LBER_ERROR ) {
327 ld->ld_errno = LDAP_DECODING_ERROR;
331 /* if it's been abandoned, toss it */
332 if ( ldap_abandoned( ld, id ) ) {
334 return( -2 ); /* continue looking */
337 if (( lr = ldap_find_request_by_msgid( ld, id )) == NULL ) {
338 Debug( LDAP_DEBUG_ANY,
339 "no request for response with msgid %ld (tossing)\n",
342 return( -2 ); /* continue looking */
345 /* the message type */
346 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
347 ld->ld_errno = LDAP_DECODING_ERROR;
352 Debug( LDAP_DEBUG_TRACE, "ldap_read: %s msgid %ld, original id %d\n",
353 ( tag == LDAP_RES_SEARCH_ENTRY ) ? "entry" :
354 ( tag == LDAP_RES_SEARCH_REFERENCE ) ? "reference" : "result",
359 hadref = simple_request = 0;
360 rc = -2; /* default is to keep looking (no response found) */
361 lr->lr_res_msgtype = tag;
363 if ( tag != LDAP_RES_SEARCH_ENTRY ) {
364 if ( ld->ld_version >= LDAP_VERSION2 &&
365 ( lr->lr_parent != NULL ||
366 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ) )
368 tmpber = *ber; /* struct copy */
369 if ( ber_scanf( &tmpber, "{iaa}", &lderr,
370 &lr->lr_res_matched, &lr->lr_res_error )
372 if ( lderr != LDAP_SUCCESS ) {
373 /* referrals are in error string */
374 refer_cnt = ldap_chase_referrals( ld, lr,
375 &lr->lr_res_error, &hadref );
378 /* save errno, message, and matched string */
379 if ( !hadref || lr->lr_res_error == NULL ) {
380 lr->lr_res_errno = ( lderr ==
381 LDAP_PARTIAL_RESULTS ) ? LDAP_SUCCESS
383 } else if ( ld->ld_errno != LDAP_SUCCESS ) {
384 lr->lr_res_errno = ld->ld_errno;
386 lr->lr_res_errno = LDAP_PARTIAL_RESULTS;
388 Debug( LDAP_DEBUG_TRACE,
389 "new result: res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
390 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
391 lr->lr_res_matched ? lr->lr_res_matched : "" );
395 Debug( LDAP_DEBUG_TRACE,
396 "read1msg: %d new referrals\n", refer_cnt, 0, 0 );
398 if ( refer_cnt != 0 ) { /* chasing referrals */
401 if ( refer_cnt < 0 ) {
402 return( -1 ); /* fatal error */
404 lr->lr_status = LDAP_REQST_CHASINGREFS;
406 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
407 /* request without any referrals */
408 simple_request = ( hadref ? 0 : 1 );
410 /* request with referrals or child request */
415 while ( lr->lr_parent != NULL ) {
416 merge_error_info( ld, lr->lr_parent, lr );
419 if ( --lr->lr_outrefcnt > 0 ) {
420 break; /* not completely done yet */
424 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
426 tag = lr->lr_res_msgtype;
427 Debug( LDAP_DEBUG_ANY, "request %ld done\n",
429 Debug( LDAP_DEBUG_TRACE,
430 "res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
431 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
432 lr->lr_res_matched ? lr->lr_res_matched : "" );
433 if ( !simple_request ) {
436 if ( build_result_ber( ld, &ber, lr )
438 rc = -1; /* fatal error */
442 ldap_free_request( ld, lr );
446 ldap_free_connection( ld, lc, 0, 1 );
455 /* make a new ldap message */
456 if ( (new = (LDAPMessage *) LDAP_CALLOC( 1, sizeof(LDAPMessage) ))
458 ld->ld_errno = LDAP_NO_MEMORY;
461 new->lm_msgid = (int)id;
462 new->lm_msgtype = tag;
466 if ( ld->ld_cache != NULL ) {
467 ldap_add_result_to_cache( ld, new );
469 #endif /* LDAP_NOCACHE */
471 /* is this the one we're looking for? */
472 if ( msgid == LDAP_RES_ANY || id == msgid ) {
474 || (new->lm_msgtype != LDAP_RES_SEARCH_RESULT
475 && new->lm_msgtype != LDAP_RES_SEARCH_ENTRY) ) {
477 ld->ld_errno = LDAP_SUCCESS;
479 } else if ( new->lm_msgtype == LDAP_RES_SEARCH_RESULT) {
480 foundit = 1; /* return the chain later */
485 * if not, we must add it to the list of responses. if
486 * the msgid is already there, it must be part of an existing
491 for ( l = ld->ld_responses; l != NULL; l = l->lm_next ) {
492 if ( l->lm_msgid == new->lm_msgid )
497 /* not part of an existing search response */
501 ld->ld_errno = LDAP_SUCCESS;
505 new->lm_next = ld->ld_responses;
506 ld->ld_responses = new;
507 return( -2 ); /* continue looking */
510 Debug( LDAP_DEBUG_TRACE, "adding response id %d type %d:\n",
511 new->lm_msgid, new->lm_msgtype, 0 );
513 /* part of a search response - add to end of list of entries */
514 for ( tmp = l; tmp->lm_chain != NULL &&
515 tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY;
516 tmp = tmp->lm_chain )
520 /* return the whole chain if that's what we were looking for */
523 ld->ld_responses = l->lm_next;
525 prev->lm_next = l->lm_next;
527 ld->ld_errno = LDAP_SUCCESS;
528 #ifdef LDAP_WORLD_P16
530 * XXX questionable fix; see text for [P16] on
531 * http://www.critical-angle.com/ldapworld/patch/
533 * inclusion of this patch causes searchs to hang on
536 return( l->lm_msgtype );
537 #else /* LDAP_WORLD_P16 */
539 #endif /* !LDAP_WORLD_P16 */
542 return( -2 ); /* continue looking */
547 build_result_ber( LDAP *ld, BerElement **bp, LDAPRequest *lr )
555 ber = ldap_alloc_ber_with_options( ld );
558 ld->ld_errno = LDAP_NO_MEMORY;
562 if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
563 lr->lr_res_msgtype, lr->lr_res_errno,
564 lr->lr_res_matched ? lr->lr_res_matched : "",
565 lr->lr_res_error ? lr->lr_res_error : "" ) == -1 ) {
567 ld->ld_errno = LDAP_ENCODING_ERROR;
569 return( LBER_ERROR );
574 if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) {
575 ld->ld_errno = LDAP_DECODING_ERROR;
577 return( LBER_ERROR );
580 if ( ber_get_int( ber, &along ) == LBER_ERROR ) {
581 ld->ld_errno = LDAP_DECODING_ERROR;
583 return( LBER_ERROR );
586 tag = ber_peek_tag( ber, &len );
588 if ( tag == LBER_ERROR ) {
589 ld->ld_errno = LDAP_DECODING_ERROR;
591 return( LBER_ERROR );
600 merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
603 * Merge error information in "lr" with "parentr" error code and string.
605 if ( lr->lr_res_errno == LDAP_PARTIAL_RESULTS ) {
606 parentr->lr_res_errno = lr->lr_res_errno;
607 if ( lr->lr_res_error != NULL ) {
608 (void)ldap_append_referral( ld, &parentr->lr_res_error,
611 } else if ( lr->lr_res_errno != LDAP_SUCCESS &&
612 parentr->lr_res_errno == LDAP_SUCCESS ) {
613 parentr->lr_res_errno = lr->lr_res_errno;
614 if ( parentr->lr_res_error != NULL ) {
615 LDAP_FREE( parentr->lr_res_error );
617 parentr->lr_res_error = lr->lr_res_error;
618 lr->lr_res_error = NULL;
619 if ( LDAP_NAME_ERROR( lr->lr_res_errno )) {
620 if ( parentr->lr_res_matched != NULL ) {
621 LDAP_FREE( parentr->lr_res_matched );
623 parentr->lr_res_matched = lr->lr_res_matched;
624 lr->lr_res_matched = NULL;
628 Debug( LDAP_DEBUG_TRACE, "merged parent (id %d) error info: ",
629 parentr->lr_msgid, 0, 0 );
630 Debug( LDAP_DEBUG_TRACE, "result errno %d, error <%s>, matched <%s>\n",
631 parentr->lr_res_errno, parentr->lr_res_error ?
632 parentr->lr_res_error : "", parentr->lr_res_matched ?
633 parentr->lr_res_matched : "" );
639 ldap_msgtype( LDAPMessage *lm )
641 assert( lm != NULL );
642 return ( lm == NULL ) ? lm->lm_msgtype : -1;
647 ldap_msgid( LDAPMessage *lm )
649 assert( lm != NULL );
651 return ( lm == NULL ) ? lm->lm_msgid : -1;
656 ldap_msgfree( LDAPMessage *lm )
661 assert( lm != NULL );
663 Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
665 for ( ; lm != NULL; lm = next ) {
667 type = lm->lm_msgtype;
668 ber_free( lm->lm_ber, 1 );
669 LDAP_FREE( (char *) lm );
676 * ldap_msgdelete - delete a message. It returns:
677 * 0 if the entire message was deleted
678 * -1 if the message was not found, or only part of it was found
681 ldap_msgdelete( LDAP *ld, int msgid )
683 LDAPMessage *lm, *prev;
685 assert( ld != NULL );
687 Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete\n", 0, 0, 0 );
690 for ( lm = ld->ld_responses; lm != NULL; lm = lm->lm_next ) {
691 if ( lm->lm_msgid == msgid )
700 ld->ld_responses = lm->lm_next;
702 prev->lm_next = lm->lm_next;
704 if ( ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY )
712 * return 1 if message msgid is waiting to be abandoned, 0 otherwise
715 ldap_abandoned( LDAP *ld, ber_int_t msgid )
719 if ( ld->ld_abandoned == NULL )
722 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
723 if ( ld->ld_abandoned[i] == msgid )
731 ldap_mark_abandoned( LDAP *ld, ber_int_t msgid )
735 if ( ld->ld_abandoned == NULL )
738 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
739 if ( ld->ld_abandoned[i] == msgid )
742 if ( ld->ld_abandoned[i] == -1 )
745 for ( ; ld->ld_abandoned[i] != -1; i++ ) {
746 ld->ld_abandoned[i] = ld->ld_abandoned[i + 1];
753 #ifdef LDAP_CONNECTIONLESS
755 cldap_getmsg( LDAP *ld, struct timeval *timeout, BerElement *ber )
761 if ( ! ber_pvt_sb_data_ready(&ld->ld_sb) ) {
762 rc = ldap_select1( ld, timeout );
763 if ( rc == -1 || rc == 0 ) {
764 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
770 /* get the next message */
771 if ( (tag = ber_get_next( &ld->ld_sb, &len, ber ))
772 != LDAP_TAG_MESSAGE ) {
773 ld->ld_errno = (tag == LBER_DEFAULT ? LDAP_SERVER_DOWN :
780 #endif /* LDAP_CONNECTIONLESS */