1 /* result.c - wait for an ldap result */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2006 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
17 * All rights reserved.
19 /* This notice applies to changes, created by or for Novell, Inc.,
20 * to preexisting works for which notices appear elsewhere in this file.
22 * Copyright (C) 1999, 2000 Novell, Inc. All Rights Reserved.
24 * THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND TREATIES.
25 * USE, MODIFICATION, AND REDISTRIBUTION OF THIS WORK IS SUBJECT TO VERSION
26 * 2.0.1 OF THE OPENLDAP PUBLIC LICENSE, A COPY OF WHICH IS AVAILABLE AT
27 * HTTP://WWW.OPENLDAP.ORG/LICENSE.HTML OR IN THE FILE "LICENSE" IN THE
28 * TOP-LEVEL DIRECTORY OF THE DISTRIBUTION. ANY USE OR EXPLOITATION OF THIS
29 * WORK OTHER THAN AS AUTHORIZED IN VERSION 2.0.1 OF THE OPENLDAP PUBLIC
30 * LICENSE, OR OTHER PRIOR WRITTEN CONSENT FROM NOVELL, COULD SUBJECT THE
31 * PERPETRATOR TO CRIMINAL AND CIVIL LIABILITY.
33 * Modification to OpenLDAP source by Novell, Inc.
34 * April 2000 sfs Add code to process V3 referrals and search results
36 * Note: A verbatim copy of version 2.0.1 of the OpenLDAP Public License
37 * can be found in the file "build/LICENSE-2.0.1" in this distribution
38 * of OpenLDAP Software.
40 /* Portions Copyright (C) The Internet Society (2006)
41 * ASN.1 fragments are from RFC 4511; see RFC for full legal notices.
46 * LDAPResult ::= SEQUENCE {
47 * resultCode ENUMERATED { ... },
49 * diagnosticMessage LDAPString,
50 * referral [3] Referral OPTIONAL
52 * Referral ::= SEQUENCE OF LDAPURL (one or more)
53 * LDAPURL ::= LDAPString (limited to URL chars)
60 #include <ac/stdlib.h>
63 #include <ac/socket.h>
64 #include <ac/string.h>
66 #include <ac/unistd.h>
71 static int ldap_abandoned LDAP_P(( LDAP *ld, ber_int_t msgid ));
72 static int ldap_mark_abandoned LDAP_P(( LDAP *ld, ber_int_t msgid ));
73 static int wait4msg LDAP_P(( LDAP *ld, ber_int_t msgid, int all, struct timeval *timeout,
74 LDAPMessage **result ));
75 static ber_tag_t try_read1msg LDAP_P(( LDAP *ld, ber_int_t msgid,
76 int all, LDAPConn **lc, LDAPMessage **result ));
77 static ber_tag_t build_result_ber LDAP_P(( LDAP *ld, BerElement **bp, LDAPRequest *lr ));
78 static void merge_error_info LDAP_P(( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr ));
79 static LDAPMessage * chkResponseList LDAP_P(( LDAP *ld, int msgid, int all));
81 #define LDAP_MSG_X_KEEP_LOOKING (-2)
85 * ldap_result - wait for an ldap result response to a message from the
86 * ldap server. If msgid is LDAP_RES_ANY (-1), any message will be
87 * accepted. If msgid is LDAP_RES_UNSOLICITED (0), any unsolicited
88 * message is accepted. Otherwise ldap_result will wait for a response
89 * with msgid. If all is LDAP_MSG_ONE (0) the first message with id
90 * msgid will be accepted, otherwise, ldap_result will wait for all
91 * responses with id msgid and then return a pointer to the entire list
92 * of messages. In general, this is only useful for search responses,
93 * which can be of three message types (zero or more entries, zero or
94 * search references, followed by an ldap result). An extension to
95 * LDAPv3 allows partial extended responses to be returned in response
96 * to any request. The type of the first message received is returned.
97 * When waiting, any messages that have been abandoned are discarded.
100 * ldap_result( s, msgid, all, timeout, result )
107 struct timeval *timeout,
108 LDAPMessage **result )
113 assert( ld != NULL );
114 assert( result != NULL );
116 Debug( LDAP_DEBUG_TRACE, "ldap_result ld %p msgid %d\n", (void *)ld, msgid, 0 );
118 #ifdef LDAP_R_COMPILE
119 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
121 lm = chkResponseList(ld, msgid, all);
124 rc = wait4msg( ld, msgid, all, timeout, result );
127 ld->ld_errno = LDAP_SUCCESS;
130 #ifdef LDAP_R_COMPILE
131 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
142 LDAPMessage *lm, **lastlm, *nextlm;
145 * Look through the list of responses we have received on
146 * this association and see if the response we're interested in
147 * is there. If it is, return it. If not, call wait4msg() to
148 * wait until it arrives or timeout occurs.
151 #ifdef LDAP_R_COMPILE
152 LDAP_PVT_THREAD_ASSERT_MUTEX_OWNER( &ld->ld_res_mutex );
155 Debug( LDAP_DEBUG_TRACE,
156 "ldap_chkResponseList ld %p msgid %d all %d\n",
157 (void *)ld, msgid, all );
159 lastlm = &ld->ld_responses;
160 for ( lm = ld->ld_responses; lm != NULL; lm = nextlm ) {
161 nextlm = lm->lm_next;
163 if ( ldap_abandoned( ld, lm->lm_msgid ) ) {
164 Debug( LDAP_DEBUG_TRACE,
165 "ldap_chkResponseList msg abandoned, msgid %d\n",
167 ldap_mark_abandoned( ld, lm->lm_msgid );
169 /* Remove this entry from list */
177 if ( msgid == LDAP_RES_ANY || lm->lm_msgid == msgid ) {
180 if ( all == LDAP_MSG_ONE || all == LDAP_MSG_RECEIVED ||
181 msgid == LDAP_RES_UNSOLICITED ) {
185 tmp = lm->lm_chain_tail;
186 if ((tmp->lm_msgtype == LDAP_RES_SEARCH_ENTRY) ||
187 (tmp->lm_msgtype == LDAP_RES_SEARCH_REFERENCE) ||
188 (tmp->lm_msgtype == LDAP_RES_INTERMEDIATE)) {
198 lastlm = &lm->lm_next;
202 /* Found an entry, remove it from the list */
203 if ( all == LDAP_MSG_ONE && lm->lm_chain != NULL ) {
204 *lastlm = lm->lm_chain;
205 lm->lm_chain->lm_next = lm->lm_next;
206 lm->lm_chain->lm_chain_tail = ( lm->lm_chain_tail != lm ) ? lm->lm_chain_tail : lm->lm_chain;
208 lm->lm_chain_tail = NULL;
210 *lastlm = lm->lm_next;
217 Debug( LDAP_DEBUG_TRACE,
218 "ldap_chkResponseList returns ld %p NULL\n", (void *)ld, 0, 0);
220 Debug( LDAP_DEBUG_TRACE,
221 "ldap_chkResponseList returns ld %p msgid %d, type 0x%02lu\n",
222 (void *)ld, lm->lm_msgid, (unsigned long) lm->lm_msgtype);
233 struct timeval *timeout,
234 LDAPMessage **result )
237 struct timeval tv = { 0 },
240 time_t start_time = 0;
244 assert( ld != NULL );
245 assert( result != NULL );
247 #ifdef LDAP_R_COMPILE
248 LDAP_PVT_THREAD_ASSERT_MUTEX_OWNER( &ld->ld_res_mutex );
252 if ( timeout == NULL ) {
253 Debug( LDAP_DEBUG_TRACE, "wait4msg ld %p msgid %d (infinite timeout)\n",
254 (void *)ld, msgid, 0 );
256 Debug( LDAP_DEBUG_TRACE, "wait4msg ld %p msgid %d (timeout %ld usec)\n",
257 (void *)ld, msgid, (long)timeout->tv_sec * 1000000 + timeout->tv_usec );
259 #endif /* LDAP_DEBUG */
261 if ( timeout == NULL ) {
267 start_time = time( NULL );
270 rc = LDAP_MSG_X_KEEP_LOOKING;
271 while ( rc == LDAP_MSG_X_KEEP_LOOKING ) {
273 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
274 Debug( LDAP_DEBUG_TRACE, "wait4msg continue ld %p msgid %d all %d\n",
275 (void *)ld, msgid, all );
276 #ifdef LDAP_R_COMPILE
277 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
279 ldap_dump_connection( ld, ld->ld_conns, 1 );
280 #ifdef LDAP_R_COMPILE
281 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
282 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
284 ldap_dump_requests_and_responses( ld );
285 #ifdef LDAP_R_COMPILE
286 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
289 #endif /* LDAP_DEBUG */
291 if ( ( *result = chkResponseList( ld, msgid, all ) ) != NULL ) {
292 rc = (*result)->lm_msgtype;
297 #ifdef LDAP_R_COMPILE
298 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
300 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
301 if ( ber_sockbuf_ctrl( lc->lconn_sb,
302 LBER_SB_OPT_DATA_READY, NULL ) )
304 #ifdef LDAP_R_COMPILE
305 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
307 rc = try_read1msg( ld, msgid, all, &lc, result );
308 #ifdef LDAP_R_COMPILE
309 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
315 #ifdef LDAP_R_COMPILE
316 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
320 rc = ldap_int_select( ld, tvp );
323 Debug( LDAP_DEBUG_TRACE,
324 "ldap_int_select returned -1: errno %d\n",
329 if ( rc == 0 || ( rc == -1 && (
330 !LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART)
331 || errno != EINTR ) ) )
333 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
339 rc = LDAP_MSG_X_KEEP_LOOKING; /* select interrupted: loop */
342 rc = LDAP_MSG_X_KEEP_LOOKING;
343 #ifdef LDAP_R_COMPILE
344 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
346 if ( ld->ld_requests &&
347 ld->ld_requests->lr_status == LDAP_REQST_WRITING &&
348 ldap_is_write_ready( ld,
349 ld->ld_requests->lr_conn->lconn_sb ) )
351 ldap_int_flush_request( ld, ld->ld_requests );
353 #ifdef LDAP_R_COMPILE
354 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
355 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
357 for ( lc = ld->ld_conns;
358 rc == LDAP_MSG_X_KEEP_LOOKING && lc != NULL; )
360 if ( lc->lconn_status == LDAP_CONNST_CONNECTED &&
361 ldap_is_read_ready( ld, lc->lconn_sb ) )
363 #ifdef LDAP_R_COMPILE
364 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
366 rc = try_read1msg( ld, msgid, all, &lc, result );
367 #ifdef LDAP_R_COMPILE
368 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
371 /* if lc gets free()'d,
372 * there's no guarantee
373 * lc->lconn_next is still
374 * sane; better restart
378 /* don't get to next conn! */
386 #ifdef LDAP_R_COMPILE
387 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
393 if ( rc == LDAP_MSG_X_KEEP_LOOKING && tvp != NULL ) {
394 tmp_time = time( NULL );
395 tv0.tv_sec -= ( tmp_time - start_time );
396 if ( tv0.tv_sec <= 0 ) {
397 rc = 0; /* timed out */
398 ld->ld_errno = LDAP_TIMEOUT;
401 tv.tv_sec = tv0.tv_sec;
403 Debug( LDAP_DEBUG_TRACE, "wait4msg ld %p %ld secs to go\n",
404 (void *)ld, (long) tv.tv_sec, 0 );
405 start_time = tmp_time;
419 LDAPMessage **result )
422 LDAPMessage *newmsg, *l, *prev;
427 LDAPRequest *lr, *tmplr;
430 int rc, refer_cnt, hadref, simple_request;
433 #ifdef LDAP_CONNECTIONLESS
434 LDAPMessage *tmp = NULL, *chain_head = NULL;
435 int moremsgs = 0, isv2 = 0;
439 * v3ref = flag for V3 referral / search reference
440 * 0 = not a ref, 1 = sucessfully chased ref, -1 = pass ref to application
448 assert( ld != NULL );
449 assert( lcp != NULL );
450 assert( *lcp != NULL );
452 #ifdef LDAP_R_COMPILE
453 LDAP_PVT_THREAD_ASSERT_MUTEX_OWNER( &ld->ld_res_mutex );
456 Debug( LDAP_DEBUG_TRACE, "read1msg: ld %p msgid %d all %d\n",
457 (void *)ld, msgid, all );
462 if ( lc->lconn_ber == NULL ) {
463 lc->lconn_ber = ldap_alloc_ber_with_options( ld );
465 if( lc->lconn_ber == NULL ) {
471 assert( LBER_VALID (ber) );
473 /* get the next message */
475 #ifdef LDAP_CONNECTIONLESS
476 if ( LDAP_IS_UDP(ld) ) {
477 struct sockaddr from;
478 ber_int_sb_read( lc->lconn_sb, &from, sizeof(struct sockaddr) );
479 if (ld->ld_options.ldo_version == LDAP_VERSION2) isv2 = 1;
483 tag = ber_get_next( lc->lconn_sb, &len, ber );
485 case LDAP_TAG_MESSAGE:
487 * We read a complete message.
488 * The connection should no longer need this ber.
490 lc->lconn_ber = NULL;
495 Debug( LDAP_DEBUG_CONNS,
496 "ber_get_next failed.\n", 0, 0, 0 );
499 if ( errno == EWOULDBLOCK ) return LDAP_MSG_X_KEEP_LOOKING;
502 if ( errno == EAGAIN ) return LDAP_MSG_X_KEEP_LOOKING;
504 ld->ld_errno = LDAP_SERVER_DOWN;
508 ld->ld_errno = LDAP_LOCAL_ERROR;
513 if ( ber_get_int( ber, &id ) == LBER_ERROR ) {
515 ld->ld_errno = LDAP_DECODING_ERROR;
519 /* if it's been abandoned, toss it */
520 if ( ldap_abandoned( ld, id ) ) {
521 Debug( LDAP_DEBUG_ANY, "abandoned ld %p msgid %ld\n",
522 (void *)ld, (long) id, 0);
525 if ( ber_sockbuf_ctrl( lc->lconn_sb, LBER_SB_OPT_DATA_READY, NULL ) ) {
528 return( LDAP_MSG_X_KEEP_LOOKING ); /* continue looking */
531 lr = ldap_find_request_by_msgid( ld, id );
533 Debug( LDAP_DEBUG_ANY,
534 "no request for response on ld %p msgid %ld (tossing)\n",
535 (void *)ld, (long)id, 0 );
538 #ifdef LDAP_CONNECTIONLESS
539 if (LDAP_IS_UDP(ld) && isv2) {
540 ber_scanf(ber, "x{");
544 /* the message type */
545 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
546 ld->ld_errno = LDAP_DECODING_ERROR;
551 Debug( LDAP_DEBUG_TRACE,
552 "read1msg: ld %p msgid %ld message type %s\n",
553 (void *)ld, (long) lr->lr_msgid, ldap_int_msgtype2str( tag ));
557 hadref = simple_request = 0;
558 rc = LDAP_MSG_X_KEEP_LOOKING; /* default is to keep looking (no response found) */
559 lr->lr_res_msgtype = tag;
562 * This code figures out if we are going to chase a
563 * referral / search reference, or pass it back to the application
565 v3ref = V3REF_NOREF; /* Assume not a V3 search reference/referral */
566 if( (tag != LDAP_RES_SEARCH_ENTRY) && (ld->ld_version > LDAP_VERSION2) ) {
567 BerElement tmpber = *ber; /* struct copy */
570 if( tag == LDAP_RES_SEARCH_REFERENCE ) {
571 /* This is a V3 search reference */
572 /* Assume we do not chase the reference,
573 * but pass it to application */
575 if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ||
576 (lr->lr_parent != NULL) )
578 /* Get the referral list */
579 if ( ber_scanf( &tmpber, "{v}", &refs ) == LBER_ERROR ) {
580 rc = LDAP_DECODING_ERROR;
583 /* Note: refs array is freed by ldap_chase_v3referrals */
584 refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
585 1, &lr->lr_res_error, &hadref );
586 if ( refer_cnt > 0 ) {
587 /* sucessfully chased reference */
588 /* If haven't got end search, set chasing referrals */
589 if ( lr->lr_status != LDAP_REQST_COMPLETED ) {
590 lr->lr_status = LDAP_REQST_CHASINGREFS;
591 Debug( LDAP_DEBUG_TRACE,
592 "read1msg: search ref chased, "
593 "mark request chasing refs, "
598 /* We sucessfully chased the reference */
599 v3ref = V3REF_SUCCESS;
605 /* Check for V3 referral */
607 char *lr_res_error = NULL;
609 if ( ber_scanf( &tmpber, "{eAA",/*}*/ &lderr,
610 &lr->lr_res_matched, &lr_res_error )
613 if ( lr_res_error != NULL ) {
614 if ( lr->lr_res_error != NULL ) {
615 (void)ldap_append_referral( ld, &lr->lr_res_error, lr_res_error );
616 LDAP_FREE( (char *)lr_res_error );
619 lr->lr_res_error = lr_res_error;
624 /* Check if V3 referral */
625 if ( ber_peek_tag( &tmpber, &len ) == LDAP_TAG_REFERRAL ) {
626 /* We have a V3 referral, assume we cannot chase it */
628 if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS)
629 || (lr->lr_parent != NULL) )
631 /* Assume referral not chased and return it to app */
634 /* Get the referral list */
635 if( ber_scanf( &tmpber, "{v}", &refs) == LBER_ERROR) {
636 rc = LDAP_DECODING_ERROR;
637 lr->lr_status = LDAP_REQST_COMPLETED;
638 Debug( LDAP_DEBUG_TRACE,
639 "read1msg: referral decode error, mark request completed, ld %p msgid %d\n",
640 (void *)ld, lr->lr_msgid, 0);
643 /* Chase the referral
644 * Note: refs arrary is freed by ldap_chase_v3referrals
646 refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
647 0, &lr->lr_res_error, &hadref );
648 lr->lr_status = LDAP_REQST_COMPLETED;
649 Debug( LDAP_DEBUG_TRACE,
650 "read1msg: referral chased, mark request completed, ld %p msgid %d\n",
651 (void *)ld, lr->lr_msgid, 0);
653 /* Referral successfully chased */
654 v3ref = V3REF_SUCCESS;
660 if( lr->lr_res_matched != NULL ) {
661 LDAP_FREE( lr->lr_res_matched );
662 lr->lr_res_matched = NULL;
665 if( lr->lr_res_error != NULL ) {
666 LDAP_FREE( lr->lr_res_error );
667 lr->lr_res_error = NULL;
673 /* All results that just return a status, i.e. don't return data
674 * go through the following code. This code also chases V2 referrals
675 * and checks if all referrals have been chased.
677 if ( (tag != LDAP_RES_SEARCH_ENTRY) && (v3ref != V3REF_TOAPP) &&
678 (tag != LDAP_RES_INTERMEDIATE ))
680 /* For a v3 search referral/reference, only come here if already chased it */
681 if ( ld->ld_version >= LDAP_VERSION2 &&
682 ( lr->lr_parent != NULL ||
683 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ) )
685 char *lr_res_error = NULL;
687 tmpber = *ber; /* struct copy */
688 if ( v3ref == V3REF_SUCCESS ) {
689 /* V3 search reference or V3 referral
690 * sucessfully chased. If this message
691 * is a search result, then it has no more
692 * outstanding referrals.
694 if ( tag == LDAP_RES_SEARCH_RESULT )
697 } else if ( ber_scanf( &tmpber, "{eAA}", &lderr,
698 &lr->lr_res_matched, &lr_res_error )
701 if ( lr_res_error != NULL ) {
702 if ( lr->lr_res_error != NULL ) {
703 (void)ldap_append_referral( ld, &lr->lr_res_error, lr_res_error );
704 LDAP_FREE( (char *)lr_res_error );
706 lr->lr_res_error = lr_res_error;
713 case LDAP_COMPARE_TRUE:
714 case LDAP_COMPARE_FALSE:
718 if ( lr->lr_res_error == NULL
719 || lr->lr_res_error[ 0 ] == '\0' )
724 /* referrals are in error string */
725 refer_cnt = ldap_chase_referrals( ld, lr,
726 &lr->lr_res_error, -1, &hadref );
727 lr->lr_status = LDAP_REQST_COMPLETED;
728 Debug( LDAP_DEBUG_TRACE,
729 "read1msg: V2 referral chased, "
730 "mark request completed, id = %d\n",
731 lr->lr_msgid, 0, 0 );
735 /* save errno, message, and matched string */
736 if ( !hadref || lr->lr_res_error == NULL ) {
737 lr->lr_res_errno = ( lderr ==
738 LDAP_PARTIAL_RESULTS ) ? LDAP_SUCCESS
741 } else if ( ld->ld_errno != LDAP_SUCCESS ) {
742 lr->lr_res_errno = ld->ld_errno;
745 lr->lr_res_errno = LDAP_PARTIAL_RESULTS;
748 Debug( LDAP_DEBUG_TRACE, "new result: "
751 "res_matched: <%s>\n",
753 lr->lr_res_error ? lr->lr_res_error : "",
754 lr->lr_res_matched ? lr->lr_res_matched : "" );
757 /* in any case, don't leave any lr_res_error 'round */
758 if ( lr_res_error ) {
759 LDAP_FREE( lr_res_error );
763 Debug( LDAP_DEBUG_TRACE,
764 "read1msg: ld %p %d new referrals\n",
765 (void *)ld, refer_cnt, 0 );
767 if ( refer_cnt != 0 ) { /* chasing referrals */
770 if ( refer_cnt < 0 ) {
771 ldap_return_request( ld, lr, 0 );
772 return( -1 ); /* fatal error */
774 lr->lr_res_errno = LDAP_SUCCESS; /* sucessfully chased referral */
777 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
778 /* request without any referrals */
779 simple_request = ( hadref ? 0 : 1 );
782 /* request with referrals or child request */
787 lr->lr_status = LDAP_REQST_COMPLETED; /* declare this request done */
788 Debug( LDAP_DEBUG_TRACE,
789 "read1msg: mark request completed, ld %p msgid %d\n",
790 (void *)ld, lr->lr_msgid, 0);
791 while ( lr->lr_parent != NULL ) {
792 merge_error_info( ld, lr->lr_parent, lr );
795 if ( --lr->lr_outrefcnt > 0 ) {
796 break; /* not completely done yet */
800 /* Check if all requests are finished, lr is now parent */
802 if ( tmplr->lr_status == LDAP_REQST_COMPLETED ) {
803 for ( tmplr = lr->lr_child;
805 tmplr = tmplr->lr_refnext )
807 if ( tmplr->lr_status != LDAP_REQST_COMPLETED ) break;
811 /* This is the parent request if the request has referrals */
812 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL &&
816 tag = lr->lr_res_msgtype;
817 Debug( LDAP_DEBUG_ANY, "request done: ld %p msgid %ld\n",
818 (void *)ld, (long) id, 0 );
819 Debug( LDAP_DEBUG_TRACE,
820 "res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
821 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
822 lr->lr_res_matched ? lr->lr_res_matched : "" );
823 if ( !simple_request ) {
826 if ( build_result_ber( ld, &ber, lr )
829 rc = -1; /* fatal error */
833 ldap_return_request( ld, lr, 1 );
838 #ifdef LDAP_R_COMPILE
839 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
841 ldap_free_connection( ld, lc, 0, 1 );
842 #ifdef LDAP_R_COMPILE
843 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
851 ldap_return_request( ld, lr, 0 );
859 /* make a new ldap message */
860 newmsg = (LDAPMessage *) LDAP_CALLOC( 1, sizeof(LDAPMessage) );
861 if ( newmsg == NULL ) {
862 ld->ld_errno = LDAP_NO_MEMORY;
865 newmsg->lm_msgid = (int)id;
866 newmsg->lm_msgtype = tag;
867 newmsg->lm_ber = ber;
868 newmsg->lm_chain_tail = newmsg;
870 #ifdef LDAP_CONNECTIONLESS
871 /* CLDAP replies all fit in a single datagram. In LDAPv2 RFC1798
872 * the responses are all a sequence wrapped in one message. In
873 * LDAPv3 each response is in its own message. The datagram must
874 * end with a SearchResult. We can't just parse each response in
875 * separate calls to try_read1msg because the header info is only
876 * present at the beginning of the datagram, not at the beginning
877 * of each response. So parse all the responses at once and queue
878 * them up, then pull off the first response to return to the
879 * caller when all parsing is complete.
881 if ( LDAP_IS_UDP(ld) ) {
882 /* If not a result, look for more */
883 if ( tag != LDAP_RES_SEARCH_RESULT ) {
887 /* LDAPv2: dup the current ber, skip past the current
888 * response, and see if there are any more after it.
890 ber = ber_dup( ber );
891 ber_scanf( ber, "x" );
892 if (ber_peek_tag(ber, &len) != LBER_DEFAULT) {
893 /* There's more - dup the ber buffer so they can all be
894 * individually freed by ldap_msgfree.
897 ber_get_option(ber, LBER_OPT_BER_REMAINING_BYTES, &len);
898 bv.bv_val = LDAP_MALLOC(len);
901 ber_read(ber, bv.bv_val, len);
903 ber_init2(ber, &bv, ld->ld_lberoptions );
907 /* LDAPv3: Just allocate a new ber. Since this is a buffered
908 * datagram, if the sockbuf is readable we still have data
911 ber = ldap_alloc_ber_with_options( ld );
912 if ( ber_sockbuf_ctrl( lc->lconn_sb, LBER_SB_OPT_DATA_READY, NULL ) ) ok = 1;
914 /* set up response chain */
916 newmsg->lm_next = ld->ld_responses;
917 ld->ld_responses = newmsg;
920 tmp->lm_chain = newmsg;
922 chain_head->lm_chain_tail = newmsg;
924 /* "ok" means there's more to parse */
926 if (isv2) goto nextresp2;
929 /* got to end of datagram without a SearchResult. Free
930 * our dup'd ber, but leave any buffer alone. For v2 case,
931 * the previous response is still using this buffer. For v3,
932 * the new ber has no buffer to free yet.
937 } else if ( moremsgs ) {
938 /* got search result, and we had multiple responses in 1 datagram.
939 * stick the result onto the end of the chain, and then pull the
940 * first response off the head of the chain.
942 tmp->lm_chain = newmsg;
943 chain_head->lm_chain_tail = newmsg;
944 *result = chkResponseList( ld, msgid, all );
945 ld->ld_errno = LDAP_SUCCESS;
946 return( (*result)->lm_msgtype );
949 #endif /* LDAP_CONNECTIONLESS */
951 /* is this the one we're looking for? */
952 if ( msgid == LDAP_RES_ANY || id == msgid ) {
953 if ( all == LDAP_MSG_ONE
954 || (newmsg->lm_msgtype != LDAP_RES_SEARCH_RESULT
955 && newmsg->lm_msgtype != LDAP_RES_SEARCH_ENTRY
956 && newmsg->lm_msgtype != LDAP_RES_SEARCH_REFERENCE) ) {
958 ld->ld_errno = LDAP_SUCCESS;
960 } else if ( newmsg->lm_msgtype == LDAP_RES_SEARCH_RESULT) {
961 foundit = 1; /* return the chain later */
966 * if not, we must add it to the list of responses. if
967 * the msgid is already there, it must be part of an existing
972 for ( l = ld->ld_responses; l != NULL; l = l->lm_next ) {
973 if ( l->lm_msgid == newmsg->lm_msgid )
978 /* not part of an existing search response */
985 newmsg->lm_next = ld->ld_responses;
986 ld->ld_responses = newmsg;
990 Debug( LDAP_DEBUG_TRACE, "adding response ld %p msgid %ld type %ld:\n",
991 (void *)ld, (long) newmsg->lm_msgid, (long) newmsg->lm_msgtype );
993 /* part of a search response - add to end of list of entries */
994 l->lm_chain_tail->lm_chain = newmsg;
995 l->lm_chain_tail = newmsg;
997 /* return the whole chain if that's what we were looking for */
1000 ld->ld_responses = l->lm_next;
1002 prev->lm_next = l->lm_next;
1008 ld->ld_errno = LDAP_SUCCESS;
1011 if ( lc && ber_sockbuf_ctrl( lc->lconn_sb, LBER_SB_OPT_DATA_READY, NULL ) ) {
1014 return( LDAP_MSG_X_KEEP_LOOKING ); /* continue looking */
1019 build_result_ber( LDAP *ld, BerElement **bp, LDAPRequest *lr )
1027 ber = ldap_alloc_ber_with_options( ld );
1030 ld->ld_errno = LDAP_NO_MEMORY;
1034 if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
1035 lr->lr_res_msgtype, lr->lr_res_errno,
1036 lr->lr_res_matched ? lr->lr_res_matched : "",
1037 lr->lr_res_error ? lr->lr_res_error : "" ) == -1 )
1039 ld->ld_errno = LDAP_ENCODING_ERROR;
1041 return( LBER_ERROR );
1044 ber_reset( ber, 1 );
1046 if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) {
1047 ld->ld_errno = LDAP_DECODING_ERROR;
1049 return( LBER_ERROR );
1052 if ( ber_get_enum( ber, &along ) == LBER_ERROR ) {
1053 ld->ld_errno = LDAP_DECODING_ERROR;
1055 return( LBER_ERROR );
1058 tag = ber_peek_tag( ber, &len );
1060 if ( tag == LBER_ERROR ) {
1061 ld->ld_errno = LDAP_DECODING_ERROR;
1063 return( LBER_ERROR );
1072 merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
1075 * Merge error information in "lr" with "parentr" error code and string.
1077 if ( lr->lr_res_errno == LDAP_PARTIAL_RESULTS ) {
1078 parentr->lr_res_errno = lr->lr_res_errno;
1079 if ( lr->lr_res_error != NULL ) {
1080 (void)ldap_append_referral( ld, &parentr->lr_res_error,
1083 } else if ( lr->lr_res_errno != LDAP_SUCCESS &&
1084 parentr->lr_res_errno == LDAP_SUCCESS )
1086 parentr->lr_res_errno = lr->lr_res_errno;
1087 if ( parentr->lr_res_error != NULL ) {
1088 LDAP_FREE( parentr->lr_res_error );
1090 parentr->lr_res_error = lr->lr_res_error;
1091 lr->lr_res_error = NULL;
1092 if ( LDAP_NAME_ERROR( lr->lr_res_errno ) ) {
1093 if ( parentr->lr_res_matched != NULL ) {
1094 LDAP_FREE( parentr->lr_res_matched );
1096 parentr->lr_res_matched = lr->lr_res_matched;
1097 lr->lr_res_matched = NULL;
1101 Debug( LDAP_DEBUG_TRACE, "merged parent (id %d) error info: ",
1102 parentr->lr_msgid, 0, 0 );
1103 Debug( LDAP_DEBUG_TRACE, "result errno %d, error <%s>, matched <%s>\n",
1104 parentr->lr_res_errno, parentr->lr_res_error ?
1105 parentr->lr_res_error : "", parentr->lr_res_matched ?
1106 parentr->lr_res_matched : "" );
1112 ldap_msgtype( LDAPMessage *lm )
1114 assert( lm != NULL );
1115 return ( lm != NULL ) ? (int)lm->lm_msgtype : -1;
1120 ldap_msgid( LDAPMessage *lm )
1122 assert( lm != NULL );
1124 return ( lm != NULL ) ? lm->lm_msgid : -1;
1128 char * ldap_int_msgtype2str( ber_tag_t tag )
1131 case LDAP_RES_ADD: return "add";
1132 case LDAP_RES_BIND: return "bind";
1133 case LDAP_RES_COMPARE: return "compare";
1134 case LDAP_RES_DELETE: return "delete";
1135 case LDAP_RES_EXTENDED: return "extended-result";
1136 case LDAP_RES_INTERMEDIATE: return "intermediate";
1137 case LDAP_RES_MODIFY: return "modify";
1138 case LDAP_RES_RENAME: return "rename";
1139 case LDAP_RES_SEARCH_ENTRY: return "search-entry";
1140 case LDAP_RES_SEARCH_REFERENCE: return "search-reference";
1141 case LDAP_RES_SEARCH_RESULT: return "search-result";
1147 ldap_msgfree( LDAPMessage *lm )
1152 Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
1154 for ( ; lm != NULL; lm = next ) {
1155 next = lm->lm_chain;
1156 type = lm->lm_msgtype;
1157 ber_free( lm->lm_ber, 1 );
1158 LDAP_FREE( (char *) lm );
1165 * ldap_msgdelete - delete a message. It returns:
1166 * 0 if the entire message was deleted
1167 * -1 if the message was not found, or only part of it was found
1170 ldap_msgdelete( LDAP *ld, int msgid )
1172 LDAPMessage *lm, *prev;
1175 assert( ld != NULL );
1177 Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete\n", 0, 0, 0 );
1180 #ifdef LDAP_R_COMPILE
1181 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
1183 for ( lm = ld->ld_responses; lm != NULL; lm = lm->lm_next ) {
1184 if ( lm->lm_msgid == msgid )
1193 ld->ld_responses = lm->lm_next;
1195 prev->lm_next = lm->lm_next;
1197 #ifdef LDAP_R_COMPILE
1198 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
1200 if ( lm && ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY )
1210 * return 1 if message msgid is waiting to be abandoned, 0 otherwise
1212 * expects ld_res_mutex to be locked
1215 ldap_abandoned( LDAP *ld, ber_int_t msgid )
1219 #ifdef LDAP_R_COMPILE
1220 LDAP_PVT_THREAD_ASSERT_MUTEX_OWNER( &ld->ld_res_mutex );
1223 if ( ld->ld_abandoned == NULL )
1226 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
1227 if ( ld->ld_abandoned[i] == msgid )
1235 * ldap_mark_abandoned
1237 * expects ld_res_mutex to be locked
1240 ldap_mark_abandoned( LDAP *ld, ber_int_t msgid )
1244 #ifdef LDAP_R_COMPILE
1245 LDAP_PVT_THREAD_ASSERT_MUTEX_OWNER( &ld->ld_res_mutex );
1248 if ( ld->ld_abandoned == NULL )
1251 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
1252 if ( ld->ld_abandoned[i] == msgid )
1255 if ( ld->ld_abandoned[i] == -1 )
1258 for ( ; ld->ld_abandoned[i] != -1; i++ ) {
1259 ld->ld_abandoned[i] = ld->ld_abandoned[i + 1];