1 /* result.c - wait for an ldap result */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 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 (1997)
41 * ASN.1 fragments are from RFC 2251; see RFC for full legal notices.
46 * LDAPResult ::= SEQUENCE {
47 * resultCode ENUMERATED { ... },
49 * errorMessage LDAPString,
50 * referral 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, Sockbuf *sb, 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;
144 * Look through the list of responses we have received on
145 * this association and see if the response we're interested in
146 * is there. If it is, return it. If not, call wait4msg() to
147 * wait until it arrives or timeout occurs.
150 Debug( LDAP_DEBUG_TRACE,
151 "ldap_chkResponseList ld %p msgid %d all %d\n",
152 (void *)ld, msgid, all );
153 lastlm = &ld->ld_responses;
154 for ( lm = ld->ld_responses; lm != NULL; lm = nextlm ) {
155 nextlm = lm->lm_next;
157 if ( ldap_abandoned( ld, lm->lm_msgid ) ) {
158 Debug( LDAP_DEBUG_TRACE,
159 "ldap_chkResponseList msg abandoned, msgid %d\n",
161 ldap_mark_abandoned( ld, lm->lm_msgid );
163 /* Remove this entry from list */
171 if ( msgid == LDAP_RES_ANY || lm->lm_msgid == msgid ) {
174 if ( all == LDAP_MSG_ONE || all == LDAP_MSG_RECEIVED ||
175 msgid == LDAP_RES_UNSOLICITED ) {
179 tmp = lm->lm_chain_tail;
180 if ((tmp->lm_msgtype == LDAP_RES_SEARCH_ENTRY) ||
181 (tmp->lm_msgtype == LDAP_RES_SEARCH_REFERENCE) ||
182 (tmp->lm_msgtype == LDAP_RES_INTERMEDIATE)) {
192 lastlm = &lm->lm_next;
196 /* Found an entry, remove it from the list */
197 if ( all == LDAP_MSG_ONE && lm->lm_chain != NULL ) {
198 *lastlm = lm->lm_chain;
199 lm->lm_chain->lm_next = lm->lm_next;
200 lm->lm_chain->lm_chain_tail = ( lm->lm_chain_tail != lm ) ? lm->lm_chain_tail : lm->lm_chain;
202 lm->lm_chain_tail = NULL;
204 *lastlm = lm->lm_next;
211 Debug( LDAP_DEBUG_TRACE,
212 "ldap_chkResponseList returns ld %p NULL\n", (void *)ld, 0, 0);
214 Debug( LDAP_DEBUG_TRACE,
215 "ldap_chkResponseList returns ld %p msgid %d, type 0x%02lu\n",
216 (void *)ld, lm->lm_msgid, (unsigned long) lm->lm_msgtype);
227 struct timeval *timeout,
228 LDAPMessage **result )
231 struct timeval tv = { 0 },
234 time_t start_time = 0;
236 LDAPConn *lc, *nextlc;
238 assert( ld != NULL );
239 assert( result != NULL );
242 if ( timeout == NULL ) {
243 Debug( LDAP_DEBUG_TRACE, "wait4msg ld %p msgid %d (infinite timeout)\n",
244 (void *)ld, msgid, 0 );
246 Debug( LDAP_DEBUG_TRACE, "wait4msg ld %p msgid %d (timeout %ld usec)\n",
247 (void *)ld, msgid, (long)timeout->tv_sec * 1000000 + timeout->tv_usec );
249 #endif /* LDAP_DEBUG */
251 if ( timeout == NULL ) {
257 start_time = time( NULL );
260 rc = LDAP_MSG_X_KEEP_LOOKING;
261 while ( rc == LDAP_MSG_X_KEEP_LOOKING ) {
263 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
264 Debug( LDAP_DEBUG_TRACE, "wait4msg continue ld %p msgid %d all %d\n",
265 (void *)ld, msgid, all );
266 ldap_dump_connection( ld, ld->ld_conns, 1 );
267 ldap_dump_requests_and_responses( ld );
269 #endif /* LDAP_DEBUG */
271 if ( (*result = chkResponseList(ld, msgid, all)) != NULL ) {
272 rc = (*result)->lm_msgtype;
277 #ifdef LDAP_R_COMPILE
278 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
280 for ( lc = ld->ld_conns; lc != NULL; lc = nextlc ) {
281 nextlc = lc->lconn_next;
282 if ( ber_sockbuf_ctrl( lc->lconn_sb,
283 LBER_SB_OPT_DATA_READY, NULL ) ) {
284 #ifdef LDAP_R_COMPILE
285 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
287 rc = try_read1msg( ld, msgid, all, lc->lconn_sb,
289 #ifdef LDAP_R_COMPILE
290 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
296 #ifdef LDAP_R_COMPILE
297 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
301 rc = ldap_int_select( ld, tvp );
304 Debug( LDAP_DEBUG_TRACE,
305 "ldap_int_select returned -1: errno %d\n",
310 if ( rc == 0 || ( rc == -1 && (
311 !LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART)
312 || errno != EINTR )))
314 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
320 rc = LDAP_MSG_X_KEEP_LOOKING; /* select interrupted: loop */
322 rc = LDAP_MSG_X_KEEP_LOOKING;
323 #ifdef LDAP_R_COMPILE
324 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
326 if ( ld->ld_requests &&
327 ld->ld_requests->lr_status == LDAP_REQST_WRITING &&
328 ldap_is_write_ready( ld,
329 ld->ld_requests->lr_conn->lconn_sb ) )
331 ldap_int_flush_request( ld, ld->ld_requests );
333 #ifdef LDAP_R_COMPILE
334 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
335 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
337 for ( lc = ld->ld_conns; rc == LDAP_MSG_X_KEEP_LOOKING && lc != NULL;
340 nextlc = lc->lconn_next;
341 if ( lc->lconn_status == LDAP_CONNST_CONNECTED &&
342 ldap_is_read_ready( ld, lc->lconn_sb ))
344 #ifdef LDAP_R_COMPILE
345 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
347 rc = try_read1msg( ld, msgid, all,
348 lc->lconn_sb, &lc, result );
349 if ( lc == NULL ) lc = nextlc;
350 #ifdef LDAP_R_COMPILE
351 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
355 #ifdef LDAP_R_COMPILE
356 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
362 if ( rc == LDAP_MSG_X_KEEP_LOOKING && tvp != NULL ) {
363 tmp_time = time( NULL );
364 tv0.tv_sec -= ( tmp_time - start_time );
365 if ( tv0.tv_sec <= 0 ) {
366 rc = 0; /* timed out */
367 ld->ld_errno = LDAP_TIMEOUT;
370 tv.tv_sec = tv0.tv_sec;
372 Debug( LDAP_DEBUG_TRACE, "wait4msg ld %p %ld secs to go\n",
373 (void *)ld, (long) tv.tv_sec, 0 );
374 start_time = tmp_time;
389 LDAPMessage **result )
392 LDAPMessage *newmsg, *l, *prev;
397 LDAPRequest *lr, *tmplr;
400 int rc, refer_cnt, hadref, simple_request;
403 #ifdef LDAP_CONNECTIONLESS
404 LDAPMessage *tmp = NULL, *chain_head = NULL;
405 int moremsgs = 0, isv2 = 0;
409 * v3ref = flag for V3 referral / search reference
410 * 0 = not a ref, 1 = sucessfully chased ref, -1 = pass ref to application
418 assert( ld != NULL );
419 assert( lcp != NULL );
420 assert( *lcp != NULL );
422 Debug( LDAP_DEBUG_TRACE, "read1msg: ld %p msgid %d all %d\n",
423 (void *)ld, msgid, all );
428 if ( lc->lconn_ber == NULL ) {
429 lc->lconn_ber = ldap_alloc_ber_with_options(ld);
431 if( lc->lconn_ber == NULL ) {
437 assert( LBER_VALID (ber) );
439 /* get the next message */
441 #ifdef LDAP_CONNECTIONLESS
442 if ( LDAP_IS_UDP(ld) ) {
443 struct sockaddr from;
444 ber_int_sb_read(sb, &from, sizeof(struct sockaddr));
445 if (ld->ld_options.ldo_version == LDAP_VERSION2) isv2=1;
449 tag = ber_get_next( sb, &len, ber );
450 if ( tag == LDAP_TAG_MESSAGE ) {
452 * We read a complete message.
453 * The connection should no longer need this ber.
455 lc->lconn_ber = NULL;
457 if ( tag != LDAP_TAG_MESSAGE ) {
458 if ( tag == LBER_DEFAULT) {
460 Debug( LDAP_DEBUG_CONNS,
461 "ber_get_next failed.\n", 0, 0, 0 );
464 if (errno==EWOULDBLOCK) return LDAP_MSG_X_KEEP_LOOKING;
467 if (errno == EAGAIN) return LDAP_MSG_X_KEEP_LOOKING;
469 ld->ld_errno = LDAP_SERVER_DOWN;
472 ld->ld_errno = LDAP_LOCAL_ERROR;
477 if ( ber_get_int( ber, &id ) == LBER_ERROR ) {
479 ld->ld_errno = LDAP_DECODING_ERROR;
483 /* if it's been abandoned, toss it */
484 if ( ldap_abandoned( ld, id ) ) {
485 Debug( LDAP_DEBUG_ANY, "abandoned ld %p msgid %ld\n",
486 (void *)ld, (long) id, 0);
489 if ( ber_sockbuf_ctrl( sb, LBER_SB_OPT_DATA_READY, NULL ) ) {
492 return( LDAP_MSG_X_KEEP_LOOKING ); /* continue looking */
495 lr = ldap_find_request_by_msgid( ld, id );
497 Debug( LDAP_DEBUG_ANY,
498 "no request for response on ld %p msgid %ld (tossing)\n",
499 (void *)ld, (long) id, 0 );
502 #ifdef LDAP_CONNECTIONLESS
503 if (LDAP_IS_UDP(ld) && isv2) {
504 ber_scanf(ber, "x{");
508 /* the message type */
509 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
510 ld->ld_errno = LDAP_DECODING_ERROR;
515 Debug( LDAP_DEBUG_TRACE,
516 "read1msg: ld %p msgid %ld message type %s\n",
517 (void *)ld, (long) lr->lr_msgid, ldap_int_msgtype2str( tag ));
521 hadref = simple_request = 0;
522 rc = LDAP_MSG_X_KEEP_LOOKING; /* default is to keep looking (no response found) */
523 lr->lr_res_msgtype = tag;
526 * This code figures out if we are going to chase a
527 * referral / search reference, or pass it back to the application
529 v3ref = V3REF_NOREF; /* Assume not a V3 search reference/referral */
530 if( (tag != LDAP_RES_SEARCH_ENTRY) && (ld->ld_version > LDAP_VERSION2) ) {
531 BerElement tmpber = *ber; /* struct copy */
534 if( tag == LDAP_RES_SEARCH_REFERENCE ) {
535 /* This is a V3 search reference */
536 /* Assume we do not chase the reference,
537 * but pass it to application */
539 if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ||
540 (lr->lr_parent != NULL) )
542 /* Get the referral list */
543 if ( ber_scanf( &tmpber, "{v}", &refs ) == LBER_ERROR ) {
544 rc = LDAP_DECODING_ERROR;
546 /* Note: refs array is freed by ldap_chase_v3referrals */
547 refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
548 1, &lr->lr_res_error, &hadref );
549 if ( refer_cnt > 0 ) {
550 /* sucessfully chased reference */
551 /* If haven't got end search, set chasing referrals */
552 if( lr->lr_status != LDAP_REQST_COMPLETED) {
553 lr->lr_status = LDAP_REQST_CHASINGREFS;
554 Debug( LDAP_DEBUG_TRACE,
555 "read1msg: search ref chased, "
556 "mark request chasing refs, "
561 /* We sucessfully chased the reference */
562 v3ref = V3REF_SUCCESS;
567 /* Check for V3 referral */
569 char *lr_res_error = NULL;
571 #ifdef LDAP_NULL_IS_NULL
572 if ( ber_scanf( &tmpber, "{eAA",/*}*/ &lderr,
573 &lr->lr_res_matched, &lr_res_error )
575 #else /* ! LDAP_NULL_IS_NULL */
576 if ( ber_scanf( &tmpber, "{eaa",/*}*/ &lderr,
577 &lr->lr_res_matched, &lr_res_error )
579 #endif /* ! LDAP_NULL_IS_NULL */
581 if ( lr_res_error != NULL ) {
582 #ifndef LDAP_NULL_IS_NULL
583 if ( lr_res_error[ 0 ] == '\0' ) {
584 LDAP_FREE( lr_res_error );
587 #endif /* ! LDAP_NULL_IS_NULL */
589 if ( lr->lr_res_error != NULL ) {
590 (void)ldap_append_referral( ld, &lr->lr_res_error, lr_res_error );
591 LDAP_FREE( (char *)lr_res_error );
594 lr->lr_res_error = lr_res_error;
600 /* Check if V3 referral */
601 if ( ber_peek_tag( &tmpber, &len ) == LDAP_TAG_REFERRAL ) {
602 /* We have a V3 referral, assume we cannot chase it */
604 if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS)
605 || (lr->lr_parent != NULL) )
607 /* Assume referral not chased and return it to app */
610 /* Get the referral list */
611 if( ber_scanf( &tmpber, "{v}", &refs) == LBER_ERROR) {
612 rc = LDAP_DECODING_ERROR;
613 lr->lr_status = LDAP_REQST_COMPLETED;
614 Debug( LDAP_DEBUG_TRACE,
615 "read1msg: referral decode error, mark request completed, ld %p msgid %d\n",
616 (void *)ld, lr->lr_msgid, 0);
618 /* Chase the referral
619 * Note: refs arrary is freed by ldap_chase_v3referrals
621 refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
622 0, &lr->lr_res_error, &hadref );
623 lr->lr_status = LDAP_REQST_COMPLETED;
624 Debug( LDAP_DEBUG_TRACE,
625 "read1msg: referral chased, mark request completed, ld %p msgid %d\n",
626 (void *)ld, lr->lr_msgid, 0);
628 /* Referral successfully chased */
629 v3ref = V3REF_SUCCESS;
635 if( lr->lr_res_matched != NULL ) {
636 LDAP_FREE( lr->lr_res_matched );
637 lr->lr_res_matched = NULL;
639 if( lr->lr_res_error != NULL ) {
640 LDAP_FREE( lr->lr_res_error );
641 lr->lr_res_error = NULL;
647 /* All results that just return a status, i.e. don't return data
648 * go through the following code. This code also chases V2 referrals
649 * and checks if all referrals have been chased.
651 if ( (tag != LDAP_RES_SEARCH_ENTRY) && (v3ref != V3REF_TOAPP) &&
652 (tag != LDAP_RES_INTERMEDIATE ))
654 /* For a v3 search referral/reference, only come here if already chased it */
655 if ( ld->ld_version >= LDAP_VERSION2 &&
656 ( lr->lr_parent != NULL ||
657 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ) )
659 char *lr_res_error = NULL;
661 tmpber = *ber; /* struct copy */
662 if ( v3ref == V3REF_SUCCESS ) {
663 /* V3 search reference or V3 referral
664 * sucessfully chased. If this message
665 * is a search result, then it has no more
666 * outstanding referrals.
668 if ( tag == LDAP_RES_SEARCH_RESULT )
670 #ifdef LDAP_NULL_IS_NULL
671 } else if ( ber_scanf( &tmpber, "{eAA}", &lderr,
672 &lr->lr_res_matched, &lr_res_error )
674 #else /* ! LDAP_NULL_IS_NULL */
675 } else if ( ber_scanf( &tmpber, "{eaa}", &lderr,
676 &lr->lr_res_matched, &lr_res_error )
678 #endif /* ! LDAP_NULL_IS_NULL */
680 if ( lr_res_error != NULL ) {
681 #ifndef LDAP_NULL_IS_NULL
682 if ( lr_res_error[ 0 ] == '\0' ) {
683 LDAP_FREE( lr_res_error );
685 #endif /* ! LDAP_NULL_IS_NULL */
687 if ( lr->lr_res_error != NULL ) {
688 (void)ldap_append_referral( ld, &lr->lr_res_error, lr_res_error );
689 LDAP_FREE( (char *)lr_res_error );
691 lr->lr_res_error = lr_res_error;
699 case LDAP_COMPARE_TRUE:
700 case LDAP_COMPARE_FALSE:
704 if ( lr->lr_res_error == NULL
705 || lr->lr_res_error[ 0 ] == '\0' )
710 /* referrals are in error string */
711 refer_cnt = ldap_chase_referrals( ld, lr,
712 &lr->lr_res_error, -1, &hadref );
713 lr->lr_status = LDAP_REQST_COMPLETED;
714 Debug( LDAP_DEBUG_TRACE,
715 "read1msg: V2 referral chased, "
716 "mark request completed, id = %d\n",
717 lr->lr_msgid, 0, 0 );
721 /* save errno, message, and matched string */
722 if ( !hadref || lr->lr_res_error == NULL ) {
723 lr->lr_res_errno = ( lderr ==
724 LDAP_PARTIAL_RESULTS ) ? LDAP_SUCCESS
726 } else if ( ld->ld_errno != LDAP_SUCCESS ) {
727 lr->lr_res_errno = ld->ld_errno;
729 lr->lr_res_errno = LDAP_PARTIAL_RESULTS;
732 Debug( LDAP_DEBUG_TRACE, "new result: "
735 "res_matched: <%s>\n",
737 lr->lr_res_error ? lr->lr_res_error : "",
738 lr->lr_res_matched ? lr->lr_res_matched : "" );
741 /* in any case, don't leave any lr_res_error 'round */
742 if ( lr_res_error ) {
743 LDAP_FREE( lr_res_error );
747 Debug( LDAP_DEBUG_TRACE,
748 "read1msg: ld %p %d new referrals\n",
749 (void *)ld, refer_cnt, 0 );
751 if ( refer_cnt != 0 ) { /* chasing referrals */
754 if ( refer_cnt < 0 ) {
755 return( -1 ); /* fatal error */
757 lr->lr_res_errno = LDAP_SUCCESS; /* sucessfully chased referral */
759 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
760 /* request without any referrals */
761 simple_request = ( hadref ? 0 : 1 );
763 /* request with referrals or child request */
768 lr->lr_status = LDAP_REQST_COMPLETED; /* declare this request done */
769 Debug( LDAP_DEBUG_TRACE,
770 "read1msg: mark request completed, ld %p msgid %d\n",
771 (void *)ld, lr->lr_msgid, 0);
772 while ( lr->lr_parent != NULL ) {
773 merge_error_info( ld, lr->lr_parent, lr );
776 if ( --lr->lr_outrefcnt > 0 ) {
777 break; /* not completely done yet */
781 /* Check if all requests are finished, lr is now parent */
783 if (tmplr->lr_status == LDAP_REQST_COMPLETED) {
784 for ( tmplr=lr->lr_child;
786 tmplr=tmplr->lr_refnext)
788 if( tmplr->lr_status != LDAP_REQST_COMPLETED) break;
792 /* This is the parent request if the request has referrals */
793 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL &&
797 tag = lr->lr_res_msgtype;
798 Debug( LDAP_DEBUG_ANY, "request done: ld %p msgid %ld\n",
799 (void *)ld, (long) id, 0 );
800 Debug( LDAP_DEBUG_TRACE,
801 "res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
802 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
803 lr->lr_res_matched ? lr->lr_res_matched : "" );
804 if ( !simple_request ) {
807 if ( build_result_ber( ld, &ber, lr )
809 rc = -1; /* fatal error */
813 #ifdef LDAP_R_COMPILE
814 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
816 ldap_free_request( ld, lr );
817 #ifdef LDAP_R_COMPILE
818 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
823 #ifdef LDAP_R_COMPILE
824 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
826 ldap_free_connection( ld, lc, 0, 1 );
827 #ifdef LDAP_R_COMPILE
828 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
839 /* make a new ldap message */
840 newmsg = (LDAPMessage *) LDAP_CALLOC( 1, sizeof(LDAPMessage) );
841 if ( newmsg == NULL ) {
842 ld->ld_errno = LDAP_NO_MEMORY;
845 newmsg->lm_msgid = (int)id;
846 newmsg->lm_msgtype = tag;
847 newmsg->lm_ber = ber;
848 newmsg->lm_chain_tail = newmsg;
850 #ifdef LDAP_CONNECTIONLESS
851 /* CLDAP replies all fit in a single datagram. In LDAPv2 RFC1798
852 * the responses are all a sequence wrapped in one message. In
853 * LDAPv3 each response is in its own message. The datagram must
854 * end with a SearchResult. We can't just parse each response in
855 * separate calls to try_read1msg because the header info is only
856 * present at the beginning of the datagram, not at the beginning
857 * of each response. So parse all the responses at once and queue
858 * them up, then pull off the first response to return to the
859 * caller when all parsing is complete.
861 if ( LDAP_IS_UDP(ld) ) {
862 /* If not a result, look for more */
863 if ( tag != LDAP_RES_SEARCH_RESULT ) {
867 /* LDAPv2: dup the current ber, skip past the current
868 * response, and see if there are any more after it.
870 ber = ber_dup( ber );
871 ber_scanf( ber, "x" );
872 if (ber_peek_tag(ber, &len) != LBER_DEFAULT) {
873 /* There's more - dup the ber buffer so they can all be
874 * individually freed by ldap_msgfree.
877 ber_get_option(ber, LBER_OPT_BER_REMAINING_BYTES, &len);
878 bv.bv_val = LDAP_MALLOC(len);
881 ber_read(ber, bv.bv_val, len);
883 ber_init2(ber, &bv, ld->ld_lberoptions );
887 /* LDAPv3: Just allocate a new ber. Since this is a buffered
888 * datagram, if the sockbuf is readable we still have data
891 ber = ldap_alloc_ber_with_options(ld);
892 if (ber_sockbuf_ctrl(sb, LBER_SB_OPT_DATA_READY, NULL)) ok=1;
894 /* set up response chain */
896 newmsg->lm_next = ld->ld_responses;
897 ld->ld_responses = newmsg;
900 tmp->lm_chain = newmsg;
902 chain_head->lm_chain_tail = newmsg;
904 /* "ok" means there's more to parse */
906 if (isv2) goto nextresp2;
909 /* got to end of datagram without a SearchResult. Free
910 * our dup'd ber, but leave any buffer alone. For v2 case,
911 * the previous response is still using this buffer. For v3,
912 * the new ber has no buffer to free yet.
917 } else if ( moremsgs ) {
918 /* got search result, and we had multiple responses in 1 datagram.
919 * stick the result onto the end of the chain, and then pull the
920 * first response off the head of the chain.
922 tmp->lm_chain = newmsg;
923 chain_head->lm_chain_tail = newmsg;
924 *result = chkResponseList( ld, msgid, all );
925 ld->ld_errno = LDAP_SUCCESS;
926 return( (*result)->lm_msgtype );
929 #endif /* LDAP_CONNECTIONLESS */
931 /* is this the one we're looking for? */
932 if ( msgid == LDAP_RES_ANY || id == msgid ) {
933 if ( all == LDAP_MSG_ONE
934 || (newmsg->lm_msgtype != LDAP_RES_SEARCH_RESULT
935 && newmsg->lm_msgtype != LDAP_RES_SEARCH_ENTRY
936 && newmsg->lm_msgtype != LDAP_RES_SEARCH_REFERENCE) ) {
938 ld->ld_errno = LDAP_SUCCESS;
940 } else if ( newmsg->lm_msgtype == LDAP_RES_SEARCH_RESULT) {
941 foundit = 1; /* return the chain later */
946 * if not, we must add it to the list of responses. if
947 * the msgid is already there, it must be part of an existing
952 for ( l = ld->ld_responses; l != NULL; l = l->lm_next ) {
953 if ( l->lm_msgid == newmsg->lm_msgid )
958 /* not part of an existing search response */
965 newmsg->lm_next = ld->ld_responses;
966 ld->ld_responses = newmsg;
970 Debug( LDAP_DEBUG_TRACE, "adding response ld %p msgid %ld type %ld:\n",
971 (void *)ld, (long) newmsg->lm_msgid, (long) newmsg->lm_msgtype );
973 /* part of a search response - add to end of list of entries */
974 l->lm_chain_tail->lm_chain = newmsg;
975 l->lm_chain_tail = newmsg;
977 /* return the whole chain if that's what we were looking for */
980 ld->ld_responses = l->lm_next;
982 prev->lm_next = l->lm_next;
988 ld->ld_errno = LDAP_SUCCESS;
991 if ( ber_sockbuf_ctrl( sb, LBER_SB_OPT_DATA_READY, NULL ) ) {
994 return( LDAP_MSG_X_KEEP_LOOKING ); /* continue looking */
999 build_result_ber( LDAP *ld, BerElement **bp, LDAPRequest *lr )
1007 ber = ldap_alloc_ber_with_options( ld );
1010 ld->ld_errno = LDAP_NO_MEMORY;
1014 if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
1015 lr->lr_res_msgtype, lr->lr_res_errno,
1016 lr->lr_res_matched ? lr->lr_res_matched : "",
1017 lr->lr_res_error ? lr->lr_res_error : "" ) == -1 )
1019 ld->ld_errno = LDAP_ENCODING_ERROR;
1021 return( LBER_ERROR );
1024 ber_reset( ber, 1 );
1026 if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) {
1027 ld->ld_errno = LDAP_DECODING_ERROR;
1029 return( LBER_ERROR );
1032 if ( ber_get_enum( ber, &along ) == LBER_ERROR ) {
1033 ld->ld_errno = LDAP_DECODING_ERROR;
1035 return( LBER_ERROR );
1038 tag = ber_peek_tag( ber, &len );
1040 if ( tag == LBER_ERROR ) {
1041 ld->ld_errno = LDAP_DECODING_ERROR;
1043 return( LBER_ERROR );
1052 merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
1055 * Merge error information in "lr" with "parentr" error code and string.
1057 if ( lr->lr_res_errno == LDAP_PARTIAL_RESULTS ) {
1058 parentr->lr_res_errno = lr->lr_res_errno;
1059 if ( lr->lr_res_error != NULL ) {
1060 (void)ldap_append_referral( ld, &parentr->lr_res_error,
1063 } else if ( lr->lr_res_errno != LDAP_SUCCESS &&
1064 parentr->lr_res_errno == LDAP_SUCCESS )
1066 parentr->lr_res_errno = lr->lr_res_errno;
1067 if ( parentr->lr_res_error != NULL ) {
1068 LDAP_FREE( parentr->lr_res_error );
1070 parentr->lr_res_error = lr->lr_res_error;
1071 lr->lr_res_error = NULL;
1072 if ( LDAP_NAME_ERROR( lr->lr_res_errno ) ) {
1073 if ( parentr->lr_res_matched != NULL ) {
1074 LDAP_FREE( parentr->lr_res_matched );
1076 parentr->lr_res_matched = lr->lr_res_matched;
1077 lr->lr_res_matched = NULL;
1081 Debug( LDAP_DEBUG_TRACE, "merged parent (id %d) error info: ",
1082 parentr->lr_msgid, 0, 0 );
1083 Debug( LDAP_DEBUG_TRACE, "result errno %d, error <%s>, matched <%s>\n",
1084 parentr->lr_res_errno, parentr->lr_res_error ?
1085 parentr->lr_res_error : "", parentr->lr_res_matched ?
1086 parentr->lr_res_matched : "" );
1092 ldap_msgtype( LDAPMessage *lm )
1094 assert( lm != NULL );
1095 return ( lm != NULL ) ? (int)lm->lm_msgtype : -1;
1100 ldap_msgid( LDAPMessage *lm )
1102 assert( lm != NULL );
1104 return ( lm != NULL ) ? lm->lm_msgid : -1;
1108 char * ldap_int_msgtype2str( ber_tag_t tag )
1111 case LDAP_RES_ADD: return "add";
1112 case LDAP_RES_BIND: return "bind";
1113 case LDAP_RES_COMPARE: return "compare";
1114 case LDAP_RES_DELETE: return "delete";
1115 case LDAP_RES_EXTENDED: return "extended-result";
1116 case LDAP_RES_INTERMEDIATE: return "intermediate";
1117 case LDAP_RES_MODIFY: return "modify";
1118 case LDAP_RES_RENAME: return "rename";
1119 case LDAP_RES_SEARCH_ENTRY: return "search-entry";
1120 case LDAP_RES_SEARCH_REFERENCE: return "search-reference";
1121 case LDAP_RES_SEARCH_RESULT: return "search-result";
1127 ldap_msgfree( LDAPMessage *lm )
1132 Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
1134 for ( ; lm != NULL; lm = next ) {
1135 next = lm->lm_chain;
1136 type = lm->lm_msgtype;
1137 ber_free( lm->lm_ber, 1 );
1138 LDAP_FREE( (char *) lm );
1145 * ldap_msgdelete - delete a message. It returns:
1146 * 0 if the entire message was deleted
1147 * -1 if the message was not found, or only part of it was found
1150 ldap_msgdelete( LDAP *ld, int msgid )
1152 LDAPMessage *lm, *prev;
1155 assert( ld != NULL );
1157 Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete\n", 0, 0, 0 );
1160 #ifdef LDAP_R_COMPILE
1161 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
1163 for ( lm = ld->ld_responses; lm != NULL; lm = lm->lm_next ) {
1164 if ( lm->lm_msgid == msgid )
1173 ld->ld_responses = lm->lm_next;
1175 prev->lm_next = lm->lm_next;
1177 #ifdef LDAP_R_COMPILE
1178 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
1180 if ( lm && ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY )
1188 * return 1 if message msgid is waiting to be abandoned, 0 otherwise
1191 ldap_abandoned( LDAP *ld, ber_int_t msgid )
1195 if ( ld->ld_abandoned == NULL )
1198 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
1199 if ( ld->ld_abandoned[i] == msgid )
1207 ldap_mark_abandoned( LDAP *ld, ber_int_t msgid )
1211 if ( ld->ld_abandoned == NULL )
1214 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
1215 if ( ld->ld_abandoned[i] == msgid )
1218 if ( ld->ld_abandoned[i] == -1 )
1221 for ( ; ld->ld_abandoned[i] != -1; i++ ) {
1222 ld->ld_abandoned[i] = ld->ld_abandoned[i + 1];