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_idx LDAP_P(( LDAP *ld, ber_int_t msgid ));
72 #define ldap_abandoned(ld, msgid) ( ldap_abandoned_idx((ld), (msgid)) > -1 )
73 static int ldap_mark_abandoned LDAP_P(( LDAP *ld, ber_int_t msgid ));
74 static int wait4msg LDAP_P(( LDAP *ld, ber_int_t msgid, int all, struct timeval *timeout,
75 LDAPMessage **result ));
76 static ber_tag_t try_read1msg LDAP_P(( LDAP *ld, ber_int_t msgid,
77 int all, LDAPConn **lc, LDAPMessage **result ));
78 static ber_tag_t build_result_ber LDAP_P(( LDAP *ld, BerElement **bp, LDAPRequest *lr ));
79 static void merge_error_info LDAP_P(( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr ));
80 static LDAPMessage * chkResponseList LDAP_P(( LDAP *ld, int msgid, int all));
82 #define LDAP_MSG_X_KEEP_LOOKING (-2)
86 * ldap_result - wait for an ldap result response to a message from the
87 * ldap server. If msgid is LDAP_RES_ANY (-1), any message will be
88 * accepted. If msgid is LDAP_RES_UNSOLICITED (0), any unsolicited
89 * message is accepted. Otherwise ldap_result will wait for a response
90 * with msgid. If all is LDAP_MSG_ONE (0) the first message with id
91 * msgid will be accepted, otherwise, ldap_result will wait for all
92 * responses with id msgid and then return a pointer to the entire list
93 * of messages. In general, this is only useful for search responses,
94 * which can be of three message types (zero or more entries, zero or
95 * search references, followed by an ldap result). An extension to
96 * LDAPv3 allows partial extended responses to be returned in response
97 * to any request. The type of the first message received is returned.
98 * When waiting, any messages that have been abandoned are discarded.
101 * ldap_result( s, msgid, all, timeout, result )
108 struct timeval *timeout,
109 LDAPMessage **result )
111 LDAPMessage *lm = NULL;
114 assert( ld != NULL );
115 assert( result != NULL );
117 Debug( LDAP_DEBUG_TRACE, "ldap_result ld %p msgid %d\n", (void *)ld, msgid, 0 );
119 #ifdef LDAP_R_COMPILE
120 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
124 /* this is already done inside wait4msg(), right?... */
125 lm = chkResponseList( ld, msgid, all );
129 rc = wait4msg( ld, msgid, all, timeout, result );
133 ld->ld_errno = LDAP_SUCCESS;
137 #ifdef LDAP_R_COMPILE
138 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
150 LDAPMessage *lm, **lastlm, *nextlm;
154 * Look through the list of responses we have received on
155 * this association and see if the response we're interested in
156 * is there. If it is, return it. If not, call wait4msg() to
157 * wait until it arrives or timeout occurs.
160 #ifdef LDAP_R_COMPILE
161 LDAP_PVT_THREAD_ASSERT_MUTEX_OWNER( &ld->ld_res_mutex );
164 Debug( LDAP_DEBUG_TRACE,
165 "ldap_chkResponseList ld %p msgid %d all %d\n",
166 (void *)ld, msgid, all );
168 lastlm = &ld->ld_responses;
169 for ( lm = ld->ld_responses; lm != NULL; lm = nextlm ) {
170 nextlm = lm->lm_next;
173 if ( ldap_abandoned( ld, lm->lm_msgid ) ) {
174 Debug( LDAP_DEBUG_TRACE,
175 "ldap_chkResponseList msg abandoned, msgid %d\n",
177 ldap_mark_abandoned( ld, lm->lm_msgid );
179 /* Remove this entry from list */
187 if ( msgid == LDAP_RES_ANY || lm->lm_msgid == msgid ) {
190 if ( all == LDAP_MSG_ONE || all == LDAP_MSG_RECEIVED ||
191 msgid == LDAP_RES_UNSOLICITED )
196 tmp = lm->lm_chain_tail;
197 if ( (tmp->lm_msgtype == LDAP_RES_SEARCH_ENTRY) ||
198 (tmp->lm_msgtype == LDAP_RES_SEARCH_REFERENCE) ||
199 (tmp->lm_msgtype == LDAP_RES_INTERMEDIATE) )
210 lastlm = &lm->lm_next;
217 snprintf( buf, sizeof( buf ), "ld=%p msgid=%d%s cnt=%d",
218 ld, msgid, all ? " all" : "", cnt );
219 Debug( LDAP_DEBUG_TRACE, "+++ chkResponseList %s\n", buf, 0, 0 );
224 /* Found an entry, remove it from the list */
225 if ( all == LDAP_MSG_ONE && lm->lm_chain != NULL ) {
226 *lastlm = lm->lm_chain;
227 lm->lm_chain->lm_next = lm->lm_next;
228 lm->lm_chain->lm_chain_tail = ( lm->lm_chain_tail != lm ) ? lm->lm_chain_tail : lm->lm_chain;
230 lm->lm_chain_tail = NULL;
232 *lastlm = lm->lm_next;
239 Debug( LDAP_DEBUG_TRACE,
240 "ldap_chkResponseList returns ld %p NULL\n", (void *)ld, 0, 0);
242 Debug( LDAP_DEBUG_TRACE,
243 "ldap_chkResponseList returns ld %p msgid %d, type 0x%02lu\n",
244 (void *)ld, lm->lm_msgid, (unsigned long) lm->lm_msgtype );
255 struct timeval *timeout,
256 LDAPMessage **result )
259 struct timeval tv = { 0 },
262 time_t start_time = 0;
266 assert( ld != NULL );
267 assert( result != NULL );
269 #ifdef LDAP_R_COMPILE
270 LDAP_PVT_THREAD_ASSERT_MUTEX_OWNER( &ld->ld_res_mutex );
274 if ( timeout == NULL ) {
275 Debug( LDAP_DEBUG_TRACE, "wait4msg ld %p msgid %d (infinite timeout)\n",
276 (void *)ld, msgid, 0 );
278 Debug( LDAP_DEBUG_TRACE, "wait4msg ld %p msgid %d (timeout %ld usec)\n",
279 (void *)ld, msgid, (long)timeout->tv_sec * 1000000 + timeout->tv_usec );
281 #endif /* LDAP_DEBUG */
283 if ( timeout == NULL ) {
289 start_time = time( NULL );
292 rc = LDAP_MSG_X_KEEP_LOOKING;
293 while ( rc == LDAP_MSG_X_KEEP_LOOKING ) {
295 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
296 Debug( LDAP_DEBUG_TRACE, "wait4msg continue ld %p msgid %d all %d\n",
297 (void *)ld, msgid, all );
298 #ifdef LDAP_R_COMPILE
299 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
301 ldap_dump_connection( ld, ld->ld_conns, 1 );
302 #ifdef LDAP_R_COMPILE
303 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
304 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
306 ldap_dump_requests_and_responses( ld );
307 #ifdef LDAP_R_COMPILE
308 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
311 #endif /* LDAP_DEBUG */
313 if ( ( *result = chkResponseList( ld, msgid, all ) ) != NULL ) {
314 rc = (*result)->lm_msgtype;
319 #ifdef LDAP_R_COMPILE
320 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
322 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
323 if ( ber_sockbuf_ctrl( lc->lconn_sb,
324 LBER_SB_OPT_DATA_READY, NULL ) )
326 #ifdef LDAP_R_COMPILE
327 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
329 rc = try_read1msg( ld, msgid, all, &lc, result );
330 #ifdef LDAP_R_COMPILE
331 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
337 #ifdef LDAP_R_COMPILE
338 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
342 rc = ldap_int_select( ld, tvp );
345 Debug( LDAP_DEBUG_TRACE,
346 "ldap_int_select returned -1: errno %d\n",
347 sock_errno(), 0, 0 );
351 if ( rc == 0 || ( rc == -1 && (
352 !LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART)
353 || sock_errno() != EINTR ) ) )
355 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
361 rc = LDAP_MSG_X_KEEP_LOOKING; /* select interrupted: loop */
364 rc = LDAP_MSG_X_KEEP_LOOKING;
365 #ifdef LDAP_R_COMPILE
366 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
368 if ( ld->ld_requests &&
369 ld->ld_requests->lr_status == LDAP_REQST_WRITING &&
370 ldap_is_write_ready( ld,
371 ld->ld_requests->lr_conn->lconn_sb ) )
373 ldap_int_flush_request( ld, ld->ld_requests );
375 #ifdef LDAP_R_COMPILE
376 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
377 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
379 for ( lc = ld->ld_conns;
380 rc == LDAP_MSG_X_KEEP_LOOKING && lc != NULL; )
382 if ( lc->lconn_status == LDAP_CONNST_CONNECTED &&
383 ldap_is_read_ready( ld, lc->lconn_sb ) )
385 #ifdef LDAP_R_COMPILE
386 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
388 rc = try_read1msg( ld, msgid, all, &lc, result );
389 #ifdef LDAP_R_COMPILE
390 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
393 /* if lc gets free()'d,
394 * there's no guarantee
395 * lc->lconn_next is still
396 * sane; better restart
400 /* don't get to next conn! */
408 #ifdef LDAP_R_COMPILE
409 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
415 if ( rc == LDAP_MSG_X_KEEP_LOOKING && tvp != NULL ) {
416 tmp_time = time( NULL );
417 tv0.tv_sec -= ( tmp_time - start_time );
418 if ( tv0.tv_sec <= 0 ) {
419 rc = 0; /* timed out */
420 ld->ld_errno = LDAP_TIMEOUT;
423 tv.tv_sec = tv0.tv_sec;
425 Debug( LDAP_DEBUG_TRACE, "wait4msg ld %p %ld secs to go\n",
426 (void *)ld, (long) tv.tv_sec, 0 );
427 start_time = tmp_time;
441 LDAPMessage **result )
444 LDAPMessage *newmsg, *l, *prev;
449 LDAPRequest *lr, *tmplr;
452 int rc, refer_cnt, hadref, simple_request;
455 #ifdef LDAP_CONNECTIONLESS
456 LDAPMessage *tmp = NULL, *chain_head = NULL;
457 int moremsgs = 0, isv2 = 0;
461 * v3ref = flag for V3 referral / search reference
462 * 0 = not a ref, 1 = sucessfully chased ref, -1 = pass ref to application
470 assert( ld != NULL );
471 assert( lcp != NULL );
472 assert( *lcp != NULL );
474 #ifdef LDAP_R_COMPILE
475 LDAP_PVT_THREAD_ASSERT_MUTEX_OWNER( &ld->ld_res_mutex );
478 Debug( LDAP_DEBUG_TRACE, "read1msg: ld %p msgid %d all %d\n",
479 (void *)ld, msgid, all );
484 if ( lc->lconn_ber == NULL ) {
485 lc->lconn_ber = ldap_alloc_ber_with_options( ld );
487 if( lc->lconn_ber == NULL ) {
493 assert( LBER_VALID (ber) );
495 /* get the next message */
497 #ifdef LDAP_CONNECTIONLESS
498 if ( LDAP_IS_UDP(ld) ) {
499 struct sockaddr from;
500 ber_int_sb_read( lc->lconn_sb, &from, sizeof(struct sockaddr) );
501 if (ld->ld_options.ldo_version == LDAP_VERSION2) isv2 = 1;
505 tag = ber_get_next( lc->lconn_sb, &len, ber );
507 case LDAP_TAG_MESSAGE:
509 * We read a complete message.
510 * The connection should no longer need this ber.
512 lc->lconn_ber = NULL;
517 Debug( LDAP_DEBUG_CONNS,
518 "ber_get_next failed.\n", 0, 0, 0 );
521 if ( sock_errno() == EWOULDBLOCK ) return LDAP_MSG_X_KEEP_LOOKING;
524 if ( sock_errno() == EAGAIN ) return LDAP_MSG_X_KEEP_LOOKING;
526 ld->ld_errno = LDAP_SERVER_DOWN;
530 ld->ld_errno = LDAP_LOCAL_ERROR;
535 if ( ber_get_int( ber, &id ) == LBER_ERROR ) {
537 ld->ld_errno = LDAP_DECODING_ERROR;
541 /* if it's been abandoned, toss it */
542 if ( ldap_abandoned( ld, id ) ) {
543 Debug( LDAP_DEBUG_ANY, "abandoned ld %p msgid %ld\n",
544 (void *)ld, (long) id, 0);
547 if ( ber_sockbuf_ctrl( lc->lconn_sb, LBER_SB_OPT_DATA_READY, NULL ) ) {
550 return( LDAP_MSG_X_KEEP_LOOKING ); /* continue looking */
553 lr = ldap_find_request_by_msgid( ld, id );
555 Debug( LDAP_DEBUG_ANY,
556 "no request for response on ld %p msgid %ld (tossing)\n",
557 (void *)ld, (long)id, 0 );
560 #ifdef LDAP_CONNECTIONLESS
561 if (LDAP_IS_UDP(ld) && isv2) {
562 ber_scanf(ber, "x{");
566 /* the message type */
567 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
568 ld->ld_errno = LDAP_DECODING_ERROR;
573 Debug( LDAP_DEBUG_TRACE,
574 "read1msg: ld %p msgid %ld message type %s\n",
575 (void *)ld, (long) lr->lr_msgid, ldap_int_msgtype2str( tag ));
579 hadref = simple_request = 0;
580 rc = LDAP_MSG_X_KEEP_LOOKING; /* default is to keep looking (no response found) */
581 lr->lr_res_msgtype = tag;
584 * This code figures out if we are going to chase a
585 * referral / search reference, or pass it back to the application
587 v3ref = V3REF_NOREF; /* Assume not a V3 search reference/referral */
588 if( (tag != LDAP_RES_SEARCH_ENTRY) && (ld->ld_version > LDAP_VERSION2) ) {
589 BerElement tmpber = *ber; /* struct copy */
592 if( tag == LDAP_RES_SEARCH_REFERENCE ) {
593 /* This is a V3 search reference */
594 /* Assume we do not chase the reference,
595 * but pass it to application */
597 if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ||
598 (lr->lr_parent != NULL) )
600 /* Get the referral list */
601 if ( ber_scanf( &tmpber, "{v}", &refs ) == LBER_ERROR ) {
602 rc = LDAP_DECODING_ERROR;
605 /* Note: refs array is freed by ldap_chase_v3referrals */
606 refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
607 1, &lr->lr_res_error, &hadref );
608 if ( refer_cnt > 0 ) {
609 /* sucessfully chased reference */
610 /* If haven't got end search, set chasing referrals */
611 if ( lr->lr_status != LDAP_REQST_COMPLETED ) {
612 lr->lr_status = LDAP_REQST_CHASINGREFS;
613 Debug( LDAP_DEBUG_TRACE,
614 "read1msg: search ref chased, "
615 "mark request chasing refs, "
620 /* We sucessfully chased the reference */
621 v3ref = V3REF_SUCCESS;
627 /* Check for V3 referral */
629 char *lr_res_error = NULL;
631 if ( ber_scanf( &tmpber, "{eAA",/*}*/ &lderr,
632 &lr->lr_res_matched, &lr_res_error )
635 if ( lr_res_error != NULL ) {
636 if ( lr->lr_res_error != NULL ) {
637 (void)ldap_append_referral( ld, &lr->lr_res_error, lr_res_error );
638 LDAP_FREE( (char *)lr_res_error );
641 lr->lr_res_error = lr_res_error;
646 /* Check if V3 referral */
647 if ( ber_peek_tag( &tmpber, &len ) == LDAP_TAG_REFERRAL ) {
648 /* We have a V3 referral, assume we cannot chase it */
650 if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS)
651 || (lr->lr_parent != NULL) )
653 /* Assume referral not chased and return it to app */
656 /* Get the referral list */
657 if( ber_scanf( &tmpber, "{v}", &refs) == LBER_ERROR) {
658 rc = LDAP_DECODING_ERROR;
659 lr->lr_status = LDAP_REQST_COMPLETED;
660 Debug( LDAP_DEBUG_TRACE,
661 "read1msg: referral decode error, mark request completed, ld %p msgid %d\n",
662 (void *)ld, lr->lr_msgid, 0);
665 /* Chase the referral
666 * Note: refs arrary is freed by ldap_chase_v3referrals
668 refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
669 0, &lr->lr_res_error, &hadref );
670 lr->lr_status = LDAP_REQST_COMPLETED;
671 Debug( LDAP_DEBUG_TRACE,
672 "read1msg: referral chased, mark request completed, ld %p msgid %d\n",
673 (void *)ld, lr->lr_msgid, 0);
675 /* Referral successfully chased */
676 v3ref = V3REF_SUCCESS;
682 if( lr->lr_res_matched != NULL ) {
683 LDAP_FREE( lr->lr_res_matched );
684 lr->lr_res_matched = NULL;
687 if( lr->lr_res_error != NULL ) {
688 LDAP_FREE( lr->lr_res_error );
689 lr->lr_res_error = NULL;
695 /* All results that just return a status, i.e. don't return data
696 * go through the following code. This code also chases V2 referrals
697 * and checks if all referrals have been chased.
699 if ( (tag != LDAP_RES_SEARCH_ENTRY) && (v3ref != V3REF_TOAPP) &&
700 (tag != LDAP_RES_INTERMEDIATE ))
702 /* For a v3 search referral/reference, only come here if already chased it */
703 if ( ld->ld_version >= LDAP_VERSION2 &&
704 ( lr->lr_parent != NULL ||
705 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ) )
707 char *lr_res_error = NULL;
709 tmpber = *ber; /* struct copy */
710 if ( v3ref == V3REF_SUCCESS ) {
711 /* V3 search reference or V3 referral
712 * sucessfully chased. If this message
713 * is a search result, then it has no more
714 * outstanding referrals.
716 if ( tag == LDAP_RES_SEARCH_RESULT )
719 } else if ( ber_scanf( &tmpber, "{eAA}", &lderr,
720 &lr->lr_res_matched, &lr_res_error )
723 if ( lr_res_error != NULL ) {
724 if ( lr->lr_res_error != NULL ) {
725 (void)ldap_append_referral( ld, &lr->lr_res_error, lr_res_error );
726 LDAP_FREE( (char *)lr_res_error );
728 lr->lr_res_error = lr_res_error;
735 case LDAP_COMPARE_TRUE:
736 case LDAP_COMPARE_FALSE:
740 if ( lr->lr_res_error == NULL
741 || lr->lr_res_error[ 0 ] == '\0' )
746 /* referrals are in error string */
747 refer_cnt = ldap_chase_referrals( ld, lr,
748 &lr->lr_res_error, -1, &hadref );
749 lr->lr_status = LDAP_REQST_COMPLETED;
750 Debug( LDAP_DEBUG_TRACE,
751 "read1msg: V2 referral chased, "
752 "mark request completed, id = %d\n",
753 lr->lr_msgid, 0, 0 );
757 /* save errno, message, and matched string */
758 if ( !hadref || lr->lr_res_error == NULL ) {
759 lr->lr_res_errno = ( lderr ==
760 LDAP_PARTIAL_RESULTS ) ? LDAP_SUCCESS
763 } else if ( ld->ld_errno != LDAP_SUCCESS ) {
764 lr->lr_res_errno = ld->ld_errno;
767 lr->lr_res_errno = LDAP_PARTIAL_RESULTS;
770 Debug( LDAP_DEBUG_TRACE, "new result: "
773 "res_matched: <%s>\n",
775 lr->lr_res_error ? lr->lr_res_error : "",
776 lr->lr_res_matched ? lr->lr_res_matched : "" );
779 /* in any case, don't leave any lr_res_error 'round */
780 if ( lr_res_error ) {
781 LDAP_FREE( lr_res_error );
785 Debug( LDAP_DEBUG_TRACE,
786 "read1msg: ld %p %d new referrals\n",
787 (void *)ld, refer_cnt, 0 );
789 if ( refer_cnt != 0 ) { /* chasing referrals */
792 if ( refer_cnt < 0 ) {
793 ldap_return_request( ld, lr, 0 );
794 return( -1 ); /* fatal error */
796 lr->lr_res_errno = LDAP_SUCCESS; /* sucessfully chased referral */
799 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
800 /* request without any referrals */
801 simple_request = ( hadref ? 0 : 1 );
804 /* request with referrals or child request */
809 lr->lr_status = LDAP_REQST_COMPLETED; /* declare this request done */
810 Debug( LDAP_DEBUG_TRACE,
811 "read1msg: mark request completed, ld %p msgid %d\n",
812 (void *)ld, lr->lr_msgid, 0);
813 while ( lr->lr_parent != NULL ) {
814 merge_error_info( ld, lr->lr_parent, lr );
817 if ( --lr->lr_outrefcnt > 0 ) {
818 break; /* not completely done yet */
822 /* Check if all requests are finished, lr is now parent */
824 if ( tmplr->lr_status == LDAP_REQST_COMPLETED ) {
825 for ( tmplr = lr->lr_child;
827 tmplr = tmplr->lr_refnext )
829 if ( tmplr->lr_status != LDAP_REQST_COMPLETED ) break;
833 /* This is the parent request if the request has referrals */
834 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL &&
838 tag = lr->lr_res_msgtype;
839 Debug( LDAP_DEBUG_ANY, "request done: ld %p msgid %ld\n",
840 (void *)ld, (long) id, 0 );
841 Debug( LDAP_DEBUG_TRACE,
842 "res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
843 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
844 lr->lr_res_matched ? lr->lr_res_matched : "" );
845 if ( !simple_request ) {
848 if ( build_result_ber( ld, &ber, lr )
851 rc = -1; /* fatal error */
855 ldap_return_request( ld, lr, 1 );
860 #ifdef LDAP_R_COMPILE
861 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
863 ldap_free_connection( ld, lc, 0, 1 );
864 #ifdef LDAP_R_COMPILE
865 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
873 ldap_return_request( ld, lr, 0 );
881 /* make a new ldap message */
882 newmsg = (LDAPMessage *) LDAP_CALLOC( 1, sizeof(LDAPMessage) );
883 if ( newmsg == NULL ) {
884 ld->ld_errno = LDAP_NO_MEMORY;
887 newmsg->lm_msgid = (int)id;
888 newmsg->lm_msgtype = tag;
889 newmsg->lm_ber = ber;
890 newmsg->lm_chain_tail = newmsg;
892 #ifdef LDAP_CONNECTIONLESS
893 /* CLDAP replies all fit in a single datagram. In LDAPv2 RFC1798
894 * the responses are all a sequence wrapped in one message. In
895 * LDAPv3 each response is in its own message. The datagram must
896 * end with a SearchResult. We can't just parse each response in
897 * separate calls to try_read1msg because the header info is only
898 * present at the beginning of the datagram, not at the beginning
899 * of each response. So parse all the responses at once and queue
900 * them up, then pull off the first response to return to the
901 * caller when all parsing is complete.
903 if ( LDAP_IS_UDP(ld) ) {
904 /* If not a result, look for more */
905 if ( tag != LDAP_RES_SEARCH_RESULT ) {
909 /* LDAPv2: dup the current ber, skip past the current
910 * response, and see if there are any more after it.
912 ber = ber_dup( ber );
913 ber_scanf( ber, "x" );
914 if (ber_peek_tag(ber, &len) != LBER_DEFAULT) {
915 /* There's more - dup the ber buffer so they can all be
916 * individually freed by ldap_msgfree.
919 ber_get_option(ber, LBER_OPT_BER_REMAINING_BYTES, &len);
920 bv.bv_val = LDAP_MALLOC(len);
923 ber_read(ber, bv.bv_val, len);
925 ber_init2(ber, &bv, ld->ld_lberoptions );
929 /* LDAPv3: Just allocate a new ber. Since this is a buffered
930 * datagram, if the sockbuf is readable we still have data
933 ber = ldap_alloc_ber_with_options( ld );
934 if ( ber_sockbuf_ctrl( lc->lconn_sb, LBER_SB_OPT_DATA_READY, NULL ) ) ok = 1;
936 /* set up response chain */
938 newmsg->lm_next = ld->ld_responses;
939 ld->ld_responses = newmsg;
942 tmp->lm_chain = newmsg;
944 chain_head->lm_chain_tail = newmsg;
946 /* "ok" means there's more to parse */
948 if (isv2) goto nextresp2;
951 /* got to end of datagram without a SearchResult. Free
952 * our dup'd ber, but leave any buffer alone. For v2 case,
953 * the previous response is still using this buffer. For v3,
954 * the new ber has no buffer to free yet.
959 } else if ( moremsgs ) {
960 /* got search result, and we had multiple responses in 1 datagram.
961 * stick the result onto the end of the chain, and then pull the
962 * first response off the head of the chain.
964 tmp->lm_chain = newmsg;
965 chain_head->lm_chain_tail = newmsg;
966 *result = chkResponseList( ld, msgid, all );
967 ld->ld_errno = LDAP_SUCCESS;
968 return( (*result)->lm_msgtype );
971 #endif /* LDAP_CONNECTIONLESS */
973 /* is this the one we're looking for? */
974 if ( msgid == LDAP_RES_ANY || id == msgid ) {
975 if ( all == LDAP_MSG_ONE
976 || (newmsg->lm_msgtype != LDAP_RES_SEARCH_RESULT
977 && newmsg->lm_msgtype != LDAP_RES_SEARCH_ENTRY
978 && newmsg->lm_msgtype != LDAP_RES_SEARCH_REFERENCE) ) {
980 ld->ld_errno = LDAP_SUCCESS;
982 } else if ( newmsg->lm_msgtype == LDAP_RES_SEARCH_RESULT) {
983 foundit = 1; /* return the chain later */
988 * if not, we must add it to the list of responses. if
989 * the msgid is already there, it must be part of an existing
994 for ( l = ld->ld_responses; l != NULL; l = l->lm_next ) {
995 if ( l->lm_msgid == newmsg->lm_msgid )
1000 /* not part of an existing search response */
1007 newmsg->lm_next = ld->ld_responses;
1008 ld->ld_responses = newmsg;
1012 Debug( LDAP_DEBUG_TRACE, "adding response ld %p msgid %ld type %ld:\n",
1013 (void *)ld, (long) newmsg->lm_msgid, (long) newmsg->lm_msgtype );
1015 /* part of a search response - add to end of list of entries */
1016 l->lm_chain_tail->lm_chain = newmsg;
1017 l->lm_chain_tail = newmsg;
1019 /* return the whole chain if that's what we were looking for */
1022 ld->ld_responses = l->lm_next;
1024 prev->lm_next = l->lm_next;
1030 ld->ld_errno = LDAP_SUCCESS;
1033 if ( lc && ber_sockbuf_ctrl( lc->lconn_sb, LBER_SB_OPT_DATA_READY, NULL ) ) {
1036 return( LDAP_MSG_X_KEEP_LOOKING ); /* continue looking */
1041 build_result_ber( LDAP *ld, BerElement **bp, LDAPRequest *lr )
1049 ber = ldap_alloc_ber_with_options( ld );
1052 ld->ld_errno = LDAP_NO_MEMORY;
1056 if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
1057 lr->lr_res_msgtype, lr->lr_res_errno,
1058 lr->lr_res_matched ? lr->lr_res_matched : "",
1059 lr->lr_res_error ? lr->lr_res_error : "" ) == -1 )
1061 ld->ld_errno = LDAP_ENCODING_ERROR;
1063 return( LBER_ERROR );
1066 ber_reset( ber, 1 );
1068 if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) {
1069 ld->ld_errno = LDAP_DECODING_ERROR;
1071 return( LBER_ERROR );
1074 if ( ber_get_enum( ber, &along ) == LBER_ERROR ) {
1075 ld->ld_errno = LDAP_DECODING_ERROR;
1077 return( LBER_ERROR );
1080 tag = ber_peek_tag( ber, &len );
1082 if ( tag == LBER_ERROR ) {
1083 ld->ld_errno = LDAP_DECODING_ERROR;
1085 return( LBER_ERROR );
1094 merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
1097 * Merge error information in "lr" with "parentr" error code and string.
1099 if ( lr->lr_res_errno == LDAP_PARTIAL_RESULTS ) {
1100 parentr->lr_res_errno = lr->lr_res_errno;
1101 if ( lr->lr_res_error != NULL ) {
1102 (void)ldap_append_referral( ld, &parentr->lr_res_error,
1105 } else if ( lr->lr_res_errno != LDAP_SUCCESS &&
1106 parentr->lr_res_errno == LDAP_SUCCESS )
1108 parentr->lr_res_errno = lr->lr_res_errno;
1109 if ( parentr->lr_res_error != NULL ) {
1110 LDAP_FREE( parentr->lr_res_error );
1112 parentr->lr_res_error = lr->lr_res_error;
1113 lr->lr_res_error = NULL;
1114 if ( LDAP_NAME_ERROR( lr->lr_res_errno ) ) {
1115 if ( parentr->lr_res_matched != NULL ) {
1116 LDAP_FREE( parentr->lr_res_matched );
1118 parentr->lr_res_matched = lr->lr_res_matched;
1119 lr->lr_res_matched = NULL;
1123 Debug( LDAP_DEBUG_TRACE, "merged parent (id %d) error info: ",
1124 parentr->lr_msgid, 0, 0 );
1125 Debug( LDAP_DEBUG_TRACE, "result errno %d, error <%s>, matched <%s>\n",
1126 parentr->lr_res_errno, parentr->lr_res_error ?
1127 parentr->lr_res_error : "", parentr->lr_res_matched ?
1128 parentr->lr_res_matched : "" );
1134 ldap_msgtype( LDAPMessage *lm )
1136 assert( lm != NULL );
1137 return ( lm != NULL ) ? (int)lm->lm_msgtype : -1;
1142 ldap_msgid( LDAPMessage *lm )
1144 assert( lm != NULL );
1146 return ( lm != NULL ) ? lm->lm_msgid : -1;
1150 char * ldap_int_msgtype2str( ber_tag_t tag )
1153 case LDAP_RES_ADD: return "add";
1154 case LDAP_RES_BIND: return "bind";
1155 case LDAP_RES_COMPARE: return "compare";
1156 case LDAP_RES_DELETE: return "delete";
1157 case LDAP_RES_EXTENDED: return "extended-result";
1158 case LDAP_RES_INTERMEDIATE: return "intermediate";
1159 case LDAP_RES_MODIFY: return "modify";
1160 case LDAP_RES_RENAME: return "rename";
1161 case LDAP_RES_SEARCH_ENTRY: return "search-entry";
1162 case LDAP_RES_SEARCH_REFERENCE: return "search-reference";
1163 case LDAP_RES_SEARCH_RESULT: return "search-result";
1169 ldap_msgfree( LDAPMessage *lm )
1174 Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
1176 for ( ; lm != NULL; lm = next ) {
1177 next = lm->lm_chain;
1178 type = lm->lm_msgtype;
1179 ber_free( lm->lm_ber, 1 );
1180 LDAP_FREE( (char *) lm );
1187 * ldap_msgdelete - delete a message. It returns:
1188 * 0 if the entire message was deleted
1189 * -1 if the message was not found, or only part of it was found
1192 ldap_msgdelete( LDAP *ld, int msgid )
1194 LDAPMessage *lm, *prev;
1197 assert( ld != NULL );
1199 Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete ld=%p msgid=%d\n",
1200 (void *)ld, msgid, 0 );
1202 #ifdef LDAP_R_COMPILE
1203 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
1206 for ( lm = ld->ld_responses; lm != NULL; lm = lm->lm_next ) {
1207 if ( lm->lm_msgid == msgid ) {
1217 if ( prev == NULL ) {
1218 ld->ld_responses = lm->lm_next;
1220 prev->lm_next = lm->lm_next;
1223 #ifdef LDAP_R_COMPILE
1224 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
1226 if ( lm && ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY ) {
1235 * ldap_abandoned_idx
1237 * return the location of the message id in the array of abandoned
1238 * message ids, or -1
1240 * expects ld_res_mutex to be locked
1243 ldap_abandoned_idx( LDAP *ld, ber_int_t msgid )
1248 #ifdef LDAP_R_COMPILE
1249 LDAP_PVT_THREAD_ASSERT_MUTEX_OWNER( &ld->ld_res_mutex );
1252 assert( ld->ld_nabandoned >= 0 );
1254 if ( ld->ld_abandoned == NULL || ld->ld_nabandoned == 0 ) {
1259 end = ld->ld_nabandoned - 1;
1262 if ( msgid < ld->ld_abandoned[ begin ] ) {
1266 if ( msgid > ld->ld_abandoned[ end ] ) {
1270 while ( end >= begin ) {
1271 int pos = (begin + end)/2;
1272 int curid = ld->ld_abandoned[ pos ];
1274 if ( msgid < curid ) {
1277 } else if ( msgid > curid ) {
1290 * ldap_mark_abandoned
1292 * expects ld_res_mutex to be locked
1295 ldap_mark_abandoned( LDAP *ld, ber_int_t msgid )
1299 #ifdef LDAP_R_COMPILE
1300 LDAP_PVT_THREAD_ASSERT_MUTEX_OWNER( &ld->ld_res_mutex );
1303 idx = ldap_abandoned_idx( ld, msgid );
1308 --ld->ld_nabandoned;
1309 assert( ld->ld_nabandoned >= 0 );
1310 for ( i = idx; i < ld->ld_nabandoned; i++ ) {
1311 ld->ld_abandoned[ i ] = ld->ld_abandoned[ i + 1 ];