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 )
68 struct timeval *timeout,
69 LDAPMessage **result )
71 LDAPMessage *lm, *lastlm, *nextlm;
74 assert( result != NULL );
76 Debug( LDAP_DEBUG_TRACE, "ldap_result\n", 0, 0, 0 );
82 if( result == NULL ) {
83 ld->ld_errno = LDAP_PARAM_ERROR;
88 * First, look through the list of responses we have received on
89 * this association and see if the response we're interested in
90 * is there. If it is, return it. If not, call wait4msg() to
91 * wait until it arrives or timeout occurs.
96 for ( lm = ld->ld_responses; lm != NULL; lm = nextlm ) {
99 if ( ldap_abandoned( ld, lm->lm_msgid ) ) {
100 ldap_mark_abandoned( ld, lm->lm_msgid );
102 if ( lastlm == NULL ) {
103 ld->ld_responses = lm->lm_next;
105 lastlm->lm_next = nextlm;
113 if ( msgid == LDAP_RES_ANY || lm->lm_msgid == msgid ) {
117 || (lm->lm_msgtype != LDAP_RES_SEARCH_RESULT
118 && lm->lm_msgtype != LDAP_RES_SEARCH_REFERENCE /* LDAPv3 */
119 && lm->lm_msgtype != LDAP_RES_SEARCH_ENTRY) )
122 for ( tmp = lm; tmp != NULL; tmp = tmp->lm_chain ) {
123 if ( tmp->lm_msgtype == LDAP_RES_SEARCH_RESULT )
136 return( wait4msg( ld, msgid, all, timeout, result ) );
139 if ( lastlm == NULL ) {
140 ld->ld_responses = (all == 0 && lm->lm_chain != NULL
141 ? lm->lm_chain : lm->lm_next);
143 lastlm->lm_next = (all == 0 && lm->lm_chain != NULL
144 ? lm->lm_chain : lm->lm_next);
151 ld->ld_errno = LDAP_SUCCESS;
152 return( lm->lm_msgtype );
160 struct timeval *timeout,
161 LDAPMessage **result )
164 struct timeval tv, *tvp;
165 time_t start_time = 0;
167 LDAPConn *lc, *nextlc;
169 assert( ld != NULL );
170 assert( result != NULL );
173 if ( timeout == NULL ) {
174 Debug( LDAP_DEBUG_TRACE, "wait4msg (infinite timeout)\n",
177 Debug( LDAP_DEBUG_TRACE, "wait4msg (timeout %ld sec, %ld usec)\n",
178 (long) timeout->tv_sec, (long) timeout->tv_usec, 0 );
180 #endif /* LDAP_DEBUG */
182 if ( timeout == NULL ) {
187 start_time = time( NULL );
193 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
194 ldap_dump_connection( ld, ld->ld_conns, 1 );
195 ldap_dump_requests_and_responses( ld );
197 #endif /* LDAP_DEBUG */
198 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
199 if ( ber_pvt_sb_data_ready(lc->lconn_sb) ) {
200 rc = try_read1msg( ld, msgid, all, lc->lconn_sb,
207 rc = do_ldap_select( ld, tvp );
212 Debug( LDAP_DEBUG_TRACE,
213 "do_ldap_select returned -1: errno %d\n",
218 if ( rc == 0 || ( rc == -1 && (
219 !LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART)
220 || errno != EINTR )))
222 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
228 rc = -2; /* select interrupted: loop */
231 for ( lc = ld->ld_conns; rc == -2 && lc != NULL;
233 nextlc = lc->lconn_next;
234 if ( lc->lconn_status ==
235 LDAP_CONNST_CONNECTED &&
236 ldap_is_read_ready( ld,
238 rc = try_read1msg( ld, msgid, all,
239 lc->lconn_sb, lc, result );
245 if ( rc == -2 && tvp != NULL ) {
246 tmp_time = time( NULL );
247 if (( tv.tv_sec -= ( tmp_time - start_time )) <= 0 ) {
248 rc = 0; /* timed out */
249 ld->ld_errno = LDAP_TIMEOUT;
253 Debug( LDAP_DEBUG_TRACE, "wait4msg: %ld secs to go\n",
254 (long) tv.tv_sec, 0, 0 );
255 start_time = tmp_time;
270 LDAPMessage **result )
273 LDAPMessage *new, *l, *prev, *tmp;
280 int rc, refer_cnt, hadref, simple_request;
283 assert( ld != NULL );
284 assert( lc != NULL );
286 Debug( LDAP_DEBUG_TRACE, "read1msg\n", 0, 0, 0 );
288 if ( lc->lconn_ber == NULL ) {
289 lc->lconn_ber = ldap_alloc_ber_with_options(ld);
291 if( lc->lconn_ber == NULL ) {
297 assert( BER_VALID (ber) );
299 /* get the next message */
301 if ( (tag = ber_get_next( sb, &len, ber ))
302 != LDAP_TAG_MESSAGE ) {
303 if ( tag == LBER_DEFAULT) {
305 Debug( LDAP_DEBUG_CONNS,
306 "ber_get_next failed.\n", 0, 0, 0 );
309 if (errno==EWOULDBLOCK) return -2;
312 if (errno == EAGAIN) return -2;
314 ld->ld_errno = LDAP_SERVER_DOWN;
317 ld->ld_errno = LDAP_LOCAL_ERROR;
322 * We read a complete message.
323 * The connection should no longer need this ber.
325 lc->lconn_ber = NULL;
328 if ( ber_get_int( ber, &id ) == LBER_ERROR ) {
330 ld->ld_errno = LDAP_DECODING_ERROR;
334 /* if it's been abandoned, toss it */
335 if ( ldap_abandoned( ld, id ) ) {
337 return( -2 ); /* continue looking */
340 if (( lr = ldap_find_request_by_msgid( ld, id )) == NULL ) {
341 Debug( LDAP_DEBUG_ANY,
342 "no request for response with msgid %ld (tossing)\n",
345 return( -2 ); /* continue looking */
348 /* the message type */
349 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
350 ld->ld_errno = LDAP_DECODING_ERROR;
355 Debug( LDAP_DEBUG_TRACE, "ldap_read: %s msgid %ld, original id %d\n",
356 ( tag == LDAP_RES_SEARCH_ENTRY ) ? "entry" :
357 ( tag == LDAP_RES_SEARCH_REFERENCE ) ? "reference" : "result",
362 hadref = simple_request = 0;
363 rc = -2; /* default is to keep looking (no response found) */
364 lr->lr_res_msgtype = tag;
366 if ( tag != LDAP_RES_SEARCH_ENTRY ) {
367 if ( ld->ld_version >= LDAP_VERSION2 &&
368 ( lr->lr_parent != NULL ||
369 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ) )
371 tmpber = *ber; /* struct copy */
372 if ( ber_scanf( &tmpber, "{iaa}", &lderr,
373 &lr->lr_res_matched, &lr->lr_res_error )
375 if ( lderr != LDAP_SUCCESS ) {
376 /* referrals are in error string */
377 refer_cnt = ldap_chase_referrals( ld, lr,
378 &lr->lr_res_error, &hadref );
381 /* save errno, message, and matched string */
382 if ( !hadref || lr->lr_res_error == NULL ) {
383 lr->lr_res_errno = ( lderr ==
384 LDAP_PARTIAL_RESULTS ) ? LDAP_SUCCESS
386 } else if ( ld->ld_errno != LDAP_SUCCESS ) {
387 lr->lr_res_errno = ld->ld_errno;
389 lr->lr_res_errno = LDAP_PARTIAL_RESULTS;
391 Debug( LDAP_DEBUG_TRACE,
392 "new result: res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
393 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
394 lr->lr_res_matched ? lr->lr_res_matched : "" );
398 Debug( LDAP_DEBUG_TRACE,
399 "read1msg: %d new referrals\n", refer_cnt, 0, 0 );
401 if ( refer_cnt != 0 ) { /* chasing referrals */
404 if ( refer_cnt < 0 ) {
405 return( -1 ); /* fatal error */
407 lr->lr_status = LDAP_REQST_CHASINGREFS;
409 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
410 /* request without any referrals */
411 simple_request = ( hadref ? 0 : 1 );
413 /* request with referrals or child request */
418 while ( lr->lr_parent != NULL ) {
419 merge_error_info( ld, lr->lr_parent, lr );
422 if ( --lr->lr_outrefcnt > 0 ) {
423 break; /* not completely done yet */
427 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
429 tag = lr->lr_res_msgtype;
430 Debug( LDAP_DEBUG_ANY, "request %ld done\n",
432 Debug( LDAP_DEBUG_TRACE,
433 "res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
434 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
435 lr->lr_res_matched ? lr->lr_res_matched : "" );
436 if ( !simple_request ) {
439 if ( build_result_ber( ld, &ber, lr )
441 rc = -1; /* fatal error */
445 ldap_free_request( ld, lr );
449 ldap_free_connection( ld, lc, 0, 1 );
458 /* make a new ldap message */
459 if ( (new = (LDAPMessage *) LDAP_CALLOC( 1, sizeof(LDAPMessage) ))
461 ld->ld_errno = LDAP_NO_MEMORY;
464 new->lm_msgid = (int)id;
465 new->lm_msgtype = tag;
469 if ( ld->ld_cache != NULL ) {
470 ldap_add_result_to_cache( ld, new );
472 #endif /* LDAP_NOCACHE */
474 /* is this the one we're looking for? */
475 if ( msgid == LDAP_RES_ANY || id == msgid ) {
477 || (new->lm_msgtype != LDAP_RES_SEARCH_RESULT
478 && new->lm_msgtype != LDAP_RES_SEARCH_ENTRY) ) {
480 ld->ld_errno = LDAP_SUCCESS;
482 } else if ( new->lm_msgtype == LDAP_RES_SEARCH_RESULT) {
483 foundit = 1; /* return the chain later */
488 * if not, we must add it to the list of responses. if
489 * the msgid is already there, it must be part of an existing
494 for ( l = ld->ld_responses; l != NULL; l = l->lm_next ) {
495 if ( l->lm_msgid == new->lm_msgid )
500 /* not part of an existing search response */
504 ld->ld_errno = LDAP_SUCCESS;
508 new->lm_next = ld->ld_responses;
509 ld->ld_responses = new;
510 return( -2 ); /* continue looking */
513 Debug( LDAP_DEBUG_TRACE, "adding response id %d type %d:\n",
514 new->lm_msgid, new->lm_msgtype, 0 );
516 /* part of a search response - add to end of list of entries */
517 for ( tmp = l; tmp->lm_chain != NULL &&
518 tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY;
519 tmp = tmp->lm_chain )
523 /* return the whole chain if that's what we were looking for */
526 ld->ld_responses = l->lm_next;
528 prev->lm_next = l->lm_next;
530 ld->ld_errno = LDAP_SUCCESS;
531 #ifdef LDAP_WORLD_P16
533 * XXX questionable fix; see text for [P16] on
534 * http://www.critical-angle.com/ldapworld/patch/
536 * inclusion of this patch causes searchs to hang on
539 return( l->lm_msgtype );
540 #else /* LDAP_WORLD_P16 */
542 #endif /* !LDAP_WORLD_P16 */
545 return( -2 ); /* continue looking */
550 build_result_ber( LDAP *ld, BerElement **bp, LDAPRequest *lr )
558 ber = ldap_alloc_ber_with_options( ld );
561 ld->ld_errno = LDAP_NO_MEMORY;
565 if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
566 lr->lr_res_msgtype, lr->lr_res_errno,
567 lr->lr_res_matched ? lr->lr_res_matched : "",
568 lr->lr_res_error ? lr->lr_res_error : "" ) == -1 ) {
570 ld->ld_errno = LDAP_ENCODING_ERROR;
572 return( LBER_ERROR );
577 if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) {
578 ld->ld_errno = LDAP_DECODING_ERROR;
580 return( LBER_ERROR );
583 if ( ber_get_int( ber, &along ) == LBER_ERROR ) {
584 ld->ld_errno = LDAP_DECODING_ERROR;
586 return( LBER_ERROR );
589 tag = ber_peek_tag( ber, &len );
591 if ( tag == LBER_ERROR ) {
592 ld->ld_errno = LDAP_DECODING_ERROR;
594 return( LBER_ERROR );
603 merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
606 * Merge error information in "lr" with "parentr" error code and string.
608 if ( lr->lr_res_errno == LDAP_PARTIAL_RESULTS ) {
609 parentr->lr_res_errno = lr->lr_res_errno;
610 if ( lr->lr_res_error != NULL ) {
611 (void)ldap_append_referral( ld, &parentr->lr_res_error,
614 } else if ( lr->lr_res_errno != LDAP_SUCCESS &&
615 parentr->lr_res_errno == LDAP_SUCCESS ) {
616 parentr->lr_res_errno = lr->lr_res_errno;
617 if ( parentr->lr_res_error != NULL ) {
618 LDAP_FREE( parentr->lr_res_error );
620 parentr->lr_res_error = lr->lr_res_error;
621 lr->lr_res_error = NULL;
622 if ( LDAP_NAME_ERROR( lr->lr_res_errno )) {
623 if ( parentr->lr_res_matched != NULL ) {
624 LDAP_FREE( parentr->lr_res_matched );
626 parentr->lr_res_matched = lr->lr_res_matched;
627 lr->lr_res_matched = NULL;
631 Debug( LDAP_DEBUG_TRACE, "merged parent (id %d) error info: ",
632 parentr->lr_msgid, 0, 0 );
633 Debug( LDAP_DEBUG_TRACE, "result errno %d, error <%s>, matched <%s>\n",
634 parentr->lr_res_errno, parentr->lr_res_error ?
635 parentr->lr_res_error : "", parentr->lr_res_matched ?
636 parentr->lr_res_matched : "" );
642 ldap_msgtype( LDAPMessage *lm )
644 assert( lm != NULL );
645 return ( lm == NULL ) ? lm->lm_msgtype : -1;
650 ldap_msgid( LDAPMessage *lm )
652 assert( lm != NULL );
654 return ( lm == NULL ) ? lm->lm_msgid : -1;
659 ldap_msgfree( LDAPMessage *lm )
664 assert( lm != NULL );
666 Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
668 for ( ; lm != NULL; lm = next ) {
670 type = lm->lm_msgtype;
671 ber_free( lm->lm_ber, 1 );
672 LDAP_FREE( (char *) lm );
679 * ldap_msgdelete - delete a message. It returns:
680 * 0 if the entire message was deleted
681 * -1 if the message was not found, or only part of it was found
684 ldap_msgdelete( LDAP *ld, int msgid )
686 LDAPMessage *lm, *prev;
688 assert( ld != NULL );
690 Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete\n", 0, 0, 0 );
693 for ( lm = ld->ld_responses; lm != NULL; lm = lm->lm_next ) {
694 if ( lm->lm_msgid == msgid )
703 ld->ld_responses = lm->lm_next;
705 prev->lm_next = lm->lm_next;
707 if ( ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY )
715 * return 1 if message msgid is waiting to be abandoned, 0 otherwise
718 ldap_abandoned( LDAP *ld, ber_int_t msgid )
722 if ( ld->ld_abandoned == NULL )
725 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
726 if ( ld->ld_abandoned[i] == msgid )
734 ldap_mark_abandoned( LDAP *ld, ber_int_t msgid )
738 if ( ld->ld_abandoned == NULL )
741 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
742 if ( ld->ld_abandoned[i] == msgid )
745 if ( ld->ld_abandoned[i] == -1 )
748 for ( ; ld->ld_abandoned[i] != -1; i++ ) {
749 ld->ld_abandoned[i] = ld->ld_abandoned[i + 1];
756 #ifdef LDAP_CONNECTIONLESS
758 cldap_getmsg( LDAP *ld, struct timeval *timeout, BerElement *ber )
764 if ( ! ber_pvt_sb_data_ready(&ld->ld_sb) ) {
765 rc = ldap_select1( ld, timeout );
766 if ( rc == -1 || rc == 0 ) {
767 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
773 /* get the next message */
774 if ( (tag = ber_get_next( &ld->ld_sb, &len, ber ))
775 != LDAP_TAG_MESSAGE ) {
776 ld->ld_errno = (tag == LBER_DEFAULT ? LDAP_SERVER_DOWN :
783 #endif /* LDAP_CONNECTIONLESS */