1 /* result.c - wait for an ldap result */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 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));
83 * ldap_result - wait for an ldap result response to a message from the
84 * ldap server. If msgid is LDAP_RES_ANY (-1), any message will be
85 * accepted. If msgid is LDAP_RES_UNSOLICITED (0), any unsolicited
86 * message is accepted. Otherwise ldap_result will wait for a response
87 * with msgid. If all is LDAP_MSG_ONE (0) the first message with id
88 * msgid will be accepted, otherwise, ldap_result will wait for all
89 * responses with id msgid and then return a pointer to the entire list
90 * of messages. In general, this is only useful for search responses,
91 * which can be of three message types (zero or more entries, zero or
92 * search references, followed by an ldap result). An extension to
93 * LDAPv3 allows partial extended responses to be returned in response
94 * to any request. The type of the first message received is returned.
95 * When waiting, any messages that have been abandoned are discarded.
98 * ldap_result( s, msgid, all, timeout, result )
105 struct timeval *timeout,
106 LDAPMessage **result )
111 assert( ld != NULL );
112 assert( result != NULL );
114 Debug( LDAP_DEBUG_TRACE, "ldap_result msgid %d\n", msgid, 0, 0 );
116 #ifdef LDAP_R_COMPILE
117 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
119 lm = chkResponseList(ld, msgid, all);
122 rc = wait4msg( ld, msgid, all, timeout, result );
125 ld->ld_errno = LDAP_SUCCESS;
128 #ifdef LDAP_R_COMPILE
129 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
140 LDAPMessage *lm, *lastlm, *nextlm;
142 * Look through the list of responses we have received on
143 * this association and see if the response we're interested in
144 * is there. If it is, return it. If not, call wait4msg() to
145 * wait until it arrives or timeout occurs.
148 Debug( LDAP_DEBUG_TRACE,
149 "ldap_chkResponseList for msgid=%d, all=%d\n",
152 for ( lm = ld->ld_responses; lm != NULL; lm = nextlm ) {
153 nextlm = lm->lm_next;
155 if ( ldap_abandoned( ld, lm->lm_msgid ) ) {
156 Debug( LDAP_DEBUG_TRACE,
157 "ldap_chkResponseList msg abandoned, msgid %d\n",
159 ldap_mark_abandoned( ld, lm->lm_msgid );
161 if ( lastlm == NULL ) {
162 /* Remove first entry in list */
163 ld->ld_responses = lm->lm_next;
165 lastlm->lm_next = nextlm;
173 if ( msgid == LDAP_RES_ANY || lm->lm_msgid == msgid ) {
176 if ( all == LDAP_MSG_ONE || msgid == LDAP_RES_UNSOLICITED ) {
180 if ( lm->lm_chain == NULL ) {
181 if ((lm->lm_msgtype == LDAP_RES_SEARCH_ENTRY) ||
182 (lm->lm_msgtype == LDAP_RES_SEARCH_REFERENCE) ||
183 (lm->lm_msgtype == LDAP_RES_INTERMEDIATE)) {
189 if ((lm->lm_chain_tail->lm_chain->lm_msgtype
190 == LDAP_RES_SEARCH_ENTRY) ||
191 (lm->lm_chain_tail->lm_chain->lm_msgtype
192 == LDAP_RES_SEARCH_REFERENCE) ||
193 (lm->lm_chain_tail->lm_chain->lm_msgtype
194 == LDAP_RES_INTERMEDIATE)) {
197 tmp = lm->lm_chain_tail->lm_chain;
211 /* Found an entry, remove it from the list */
212 if ( lastlm == NULL ) {
213 ld->ld_responses = (all == LDAP_MSG_ONE && lm->lm_chain != NULL
214 ? lm->lm_chain : lm->lm_next);
216 lastlm->lm_next = (all == LDAP_MSG_ONE && lm->lm_chain != NULL
217 ? lm->lm_chain : lm->lm_next);
219 if ( all == LDAP_MSG_ONE && lm->lm_chain != NULL ) {
220 lm->lm_chain->lm_next = lm->lm_next;
228 Debug( LDAP_DEBUG_TRACE,
229 "ldap_chkResponseList returns NULL\n", 0, 0, 0);
231 Debug( LDAP_DEBUG_TRACE,
232 "ldap_chkResponseList returns msgid %d, type 0x%02lu\n",
233 lm->lm_msgid, (unsigned long) lm->lm_msgtype, 0);
244 struct timeval *timeout,
245 LDAPMessage **result )
248 struct timeval tv, *tvp;
249 time_t start_time = 0;
251 LDAPConn *lc, *nextlc;
253 assert( ld != NULL );
254 assert( result != NULL );
257 if ( timeout == NULL ) {
258 Debug( LDAP_DEBUG_TRACE, "wait4msg (infinite timeout), msgid %d\n",
261 Debug( LDAP_DEBUG_TRACE, "wait4msg (timeout %ld sec, %ld usec), msgid %d\n",
262 (long) timeout->tv_sec, (long) timeout->tv_usec, msgid );
264 #endif /* LDAP_DEBUG */
266 if ( timeout == NULL ) {
271 start_time = time( NULL );
277 Debug( LDAP_DEBUG_TRACE, "wait4msg continue, msgid %d, all %d\n",
279 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
280 ldap_dump_connection( ld, ld->ld_conns, 1 );
281 ldap_dump_requests_and_responses( ld );
283 #endif /* LDAP_DEBUG */
285 if( (*result = chkResponseList(ld, msgid, all)) != NULL ) {
286 rc = (*result)->lm_msgtype;
290 for ( lc = ld->ld_conns; lc != NULL; lc = nextlc ) {
291 nextlc = lc->lconn_next;
292 if ( ber_sockbuf_ctrl( lc->lconn_sb,
293 LBER_SB_OPT_DATA_READY, NULL ) ) {
294 rc = try_read1msg( ld, msgid, all, lc->lconn_sb,
302 rc = ldap_int_select( ld, tvp );
305 Debug( LDAP_DEBUG_TRACE,
306 "ldap_int_select returned -1: errno %d\n",
311 if ( rc == 0 || ( rc == -1 && (
312 !LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART)
313 || errno != EINTR )))
315 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
321 rc = -2; /* select interrupted: loop */
324 #ifdef LDAP_R_COMPILE
325 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
327 if ( ld->ld_requests &&
328 ld->ld_requests->lr_status == LDAP_REQST_WRITING &&
329 ldap_is_write_ready( ld,
330 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 );
336 for ( lc = ld->ld_conns; rc == -2 && lc != NULL;
338 nextlc = lc->lconn_next;
339 if ( lc->lconn_status ==
340 LDAP_CONNST_CONNECTED &&
341 ldap_is_read_ready( ld,
343 rc = try_read1msg( ld, msgid, all,
344 lc->lconn_sb, &lc, result );
345 if ( lc == NULL ) lc = nextlc;
352 if ( rc == -2 && tvp != NULL ) {
353 tmp_time = time( NULL );
354 if (( tv.tv_sec -= ( tmp_time - start_time )) <= 0 ) {
355 rc = 0; /* timed out */
356 ld->ld_errno = LDAP_TIMEOUT;
360 Debug( LDAP_DEBUG_TRACE, "wait4msg: %ld secs to go\n",
361 (long) tv.tv_sec, 0, 0 );
362 start_time = tmp_time;
377 LDAPMessage **result )
380 LDAPMessage *new, *l, *prev, *tmp, *chain_head;
385 LDAPRequest *lr, *tmplr;
388 int rc, refer_cnt, hadref, simple_request;
390 #ifdef LDAP_CONNECTIONLESS
391 int firstmsg = 1, moremsgs = 0, isv2 = 0;
394 * v3ref = flag for V3 referral / search reference
395 * 0 = not a ref, 1 = sucessfully chased ref, -1 = pass ref to application
399 assert( ld != NULL );
400 assert( lcp != NULL );
401 assert( *lcp != NULL );
403 Debug( LDAP_DEBUG_TRACE, "read1msg: msgid %d, all %d\n", msgid, all, 0 );
408 if ( lc->lconn_ber == NULL ) {
409 lc->lconn_ber = ldap_alloc_ber_with_options(ld);
411 if( lc->lconn_ber == NULL ) {
417 assert( LBER_VALID (ber) );
419 /* get the next message */
421 #ifdef LDAP_CONNECTIONLESS
422 if ( LDAP_IS_UDP(ld) ) {
423 struct sockaddr from;
424 ber_int_sb_read(sb, &from, sizeof(struct sockaddr));
425 if (ld->ld_options.ldo_version == LDAP_VERSION2) isv2=1;
429 tag = ber_get_next( sb, &len, ber );
430 if ( tag == LDAP_TAG_MESSAGE ) {
432 * We read a complete message.
433 * The connection should no longer need this ber.
435 lc->lconn_ber = NULL;
437 if ( tag != LDAP_TAG_MESSAGE ) {
438 if ( tag == LBER_DEFAULT) {
440 Debug( LDAP_DEBUG_CONNS,
441 "ber_get_next failed.\n", 0, 0, 0 );
444 if (errno==EWOULDBLOCK) return -2;
447 if (errno == EAGAIN) return -2;
449 ld->ld_errno = LDAP_SERVER_DOWN;
452 ld->ld_errno = LDAP_LOCAL_ERROR;
457 if ( ber_get_int( ber, &id ) == LBER_ERROR ) {
459 ld->ld_errno = LDAP_DECODING_ERROR;
463 /* if it's been abandoned, toss it */
464 if ( ldap_abandoned( ld, id ) ) {
465 Debug( LDAP_DEBUG_ANY, "abandoned\n", 0, 0, 0);
468 if ( ber_sockbuf_ctrl( sb, LBER_SB_OPT_DATA_READY, NULL ) ) {
471 return( -2 ); /* continue looking */
474 if (( lr = ldap_find_request_by_msgid( ld, id )) == NULL ) {
475 Debug( LDAP_DEBUG_ANY,
476 "no request for response with msgid %ld (tossing)\n",
480 #ifdef LDAP_CONNECTIONLESS
481 if (LDAP_IS_UDP(ld) && isv2) {
482 ber_scanf(ber, "x{");
486 /* the message type */
487 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
488 ld->ld_errno = LDAP_DECODING_ERROR;
493 Debug( LDAP_DEBUG_TRACE,
494 "ldap_read: message type %s msgid %ld, original id %ld\n",
495 ldap_int_msgtype2str( tag ),
496 (long) lr->lr_msgid, (long) lr->lr_origid );
500 hadref = simple_request = 0;
501 rc = -2; /* default is to keep looking (no response found) */
502 lr->lr_res_msgtype = tag;
505 * This code figures out if we are going to chase a
506 * referral / search reference, or pass it back to the application
508 v3ref = 0; /* Assume not a V3 search reference or referral */
509 if( (tag != LDAP_RES_SEARCH_ENTRY) && (ld->ld_version > LDAP_VERSION2) ) {
510 BerElement tmpber = *ber; /* struct copy */
513 if( tag == LDAP_RES_SEARCH_REFERENCE) {
514 /* This is a V3 search reference */
515 /* Assume we do not chase the reference, but pass it to application */
517 if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ||
518 (lr->lr_parent != NULL) )
520 /* Get the referral list */
521 if ( ber_scanf( &tmpber, "{v}", &refs ) == LBER_ERROR ) {
522 rc = LDAP_DECODING_ERROR;
524 /* Note: refs arrary is freed by ldap_chase_v3referrals */
525 refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
526 1, &lr->lr_res_error, &hadref );
527 if ( refer_cnt > 0 ) { /* sucessfully chased reference */
528 /* If haven't got end search, set chasing referrals */
529 if( lr->lr_status != LDAP_REQST_COMPLETED) {
530 lr->lr_status = LDAP_REQST_CHASINGREFS;
531 Debug( LDAP_DEBUG_TRACE,
532 "read1msg: search ref chased, mark request chasing refs, id = %d\n",
535 v3ref = 1; /* We sucessfully chased the reference */
540 /* Check for V3 referral */
542 if ( ber_scanf( &tmpber, "{iaa",/*}*/ &lderr,
543 &lr->lr_res_matched, &lr->lr_res_error )
545 /* Check if V3 referral */
546 if( ber_peek_tag( &tmpber, &len) == LDAP_TAG_REFERRAL ) {
547 /* We have a V3 referral, assume we cannot chase it */
549 if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS)
550 || (lr->lr_parent != NULL) )
552 v3ref = -1; /* Assume referral not chased and return it to app */
553 /* Get the referral list */
554 if( ber_scanf( &tmpber, "{v}", &refs) == LBER_ERROR) {
555 rc = LDAP_DECODING_ERROR;
556 lr->lr_status = LDAP_REQST_COMPLETED;
557 Debug( LDAP_DEBUG_TRACE,
558 "read1msg: referral decode error, mark request completed, id = %d\n",
561 /* Chase the referral
562 * Note: refs arrary is freed by ldap_chase_v3referrals
564 refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
565 0, &lr->lr_res_error, &hadref );
566 lr->lr_status = LDAP_REQST_COMPLETED;
567 Debug( LDAP_DEBUG_TRACE,
568 "read1msg: referral chased, mark request completed, id = %d\n",
571 v3ref = 1; /* Referral successfully chased */
577 if( lr->lr_res_matched != NULL ) {
578 LDAP_FREE( lr->lr_res_matched );
579 lr->lr_res_matched = NULL;
581 if( lr->lr_res_error != NULL ) {
582 LDAP_FREE( lr->lr_res_error );
583 lr->lr_res_error = NULL;
589 /* All results that just return a status, i.e. don't return data
590 * go through the following code. This code also chases V2 referrals
591 * and checks if all referrals have been chased.
593 if ( (tag != LDAP_RES_SEARCH_ENTRY) && (v3ref > -1) &&
594 (tag != LDAP_RES_INTERMEDIATE ))
596 /* For a v3 search referral/reference, only come here if already chased it */
597 if ( ld->ld_version >= LDAP_VERSION2 &&
598 ( lr->lr_parent != NULL ||
599 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ) )
601 tmpber = *ber; /* struct copy */
603 /* V3 search reference or V3 referral
604 * sucessfully chased. If this message
605 * is a search result, then it has no more
606 * outstanding referrals.
608 if ( tag == LDAP_RES_SEARCH_RESULT )
610 } else if ( ber_scanf( &tmpber, "{iaa}", &lderr,
611 &lr->lr_res_matched, &lr->lr_res_error )
613 if ( lderr != LDAP_SUCCESS ) {
614 /* referrals are in error string */
615 refer_cnt = ldap_chase_referrals( ld, lr,
616 &lr->lr_res_error, -1, &hadref );
617 lr->lr_status = LDAP_REQST_COMPLETED;
618 Debug( LDAP_DEBUG_TRACE,
619 "read1msg: V2 referral chased, mark request completed, id = %d\n", lr->lr_msgid, 0, 0);
622 /* save errno, message, and matched string */
623 if ( !hadref || lr->lr_res_error == NULL ) {
624 lr->lr_res_errno = ( lderr ==
625 LDAP_PARTIAL_RESULTS ) ? LDAP_SUCCESS
627 } else if ( ld->ld_errno != LDAP_SUCCESS ) {
628 lr->lr_res_errno = ld->ld_errno;
630 lr->lr_res_errno = LDAP_PARTIAL_RESULTS;
632 Debug( LDAP_DEBUG_TRACE,
633 "new result: res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
634 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
635 lr->lr_res_matched ? lr->lr_res_matched : "" );
639 Debug( LDAP_DEBUG_TRACE,
640 "read1msg: %d new referrals\n", refer_cnt, 0, 0 );
642 if ( refer_cnt != 0 ) { /* chasing referrals */
645 if ( refer_cnt < 0 ) {
646 return( -1 ); /* fatal error */
648 lr->lr_res_errno = LDAP_SUCCESS; /* sucessfully chased referral */
650 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
651 /* request without any referrals */
652 simple_request = ( hadref ? 0 : 1 );
654 /* request with referrals or child request */
659 lr->lr_status = LDAP_REQST_COMPLETED; /* declare this request done */
660 Debug( LDAP_DEBUG_TRACE,
661 "read1msg: mark request completed, id = %d\n", lr->lr_msgid, 0, 0);
662 while ( lr->lr_parent != NULL ) {
663 merge_error_info( ld, lr->lr_parent, lr );
666 if ( --lr->lr_outrefcnt > 0 ) {
667 break; /* not completely done yet */
671 /* Check if all requests are finished, lr is now parent */
673 if (tmplr->lr_status == LDAP_REQST_COMPLETED) {
674 for ( tmplr=lr->lr_child;
676 tmplr=tmplr->lr_refnext)
678 if( tmplr->lr_status != LDAP_REQST_COMPLETED) break;
682 /* This is the parent request if the request has referrals */
683 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL &&
687 tag = lr->lr_res_msgtype;
688 Debug( LDAP_DEBUG_ANY, "request %ld done\n",
690 Debug( LDAP_DEBUG_TRACE,
691 "res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
692 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
693 lr->lr_res_matched ? lr->lr_res_matched : "" );
694 if ( !simple_request ) {
697 if ( build_result_ber( ld, &ber, lr )
699 rc = -1; /* fatal error */
703 ldap_free_request( ld, lr );
707 ldap_free_connection( ld, lc, 0, 1 );
717 /* make a new ldap message */
718 if ( (new = (LDAPMessage *) LDAP_CALLOC( 1, sizeof(LDAPMessage) ))
720 ld->ld_errno = LDAP_NO_MEMORY;
723 new->lm_msgid = (int)id;
724 new->lm_msgtype = tag;
727 #ifdef LDAP_CONNECTIONLESS
728 /* CLDAP replies all fit in a single datagram. In LDAPv2 RFC1798
729 * the responses are all a sequence wrapped in one message. In
730 * LDAPv3 each response is in its own message. The datagram must
731 * end with a SearchResult. We can't just parse each response in
732 * separate calls to try_read1msg because the header info is only
733 * present at the beginning of the datagram, not at the beginning
734 * of each response. So parse all the responses at once and queue
735 * them up, then pull off the first response to return to the
736 * caller when all parsing is complete.
738 if ( LDAP_IS_UDP(ld) ) {
739 /* If not a result, look for more */
740 if ( tag != LDAP_RES_SEARCH_RESULT ) {
744 /* LDAPv2: dup the current ber, skip past the current
745 * response, and see if there are any more after it.
747 ber = ber_dup( ber );
748 ber_scanf( ber, "x" );
749 if (ber_peek_tag(ber, &len) != LBER_DEFAULT) {
750 /* There's more - dup the ber buffer so they can all be
751 * individually freed by ldap_msgfree.
754 ber_get_option(ber, LBER_OPT_BER_REMAINING_BYTES, &len);
755 bv.bv_val = LDAP_MALLOC(len);
758 ber_read(ber, bv.bv_val, len);
760 ber_init2(ber, &bv, ld->ld_lberoptions );
764 /* LDAPv3: Just allocate a new ber. Since this is a buffered
765 * datagram, if the sockbuf is readable we still have data
768 ber = ldap_alloc_ber_with_options(ld);
769 if (ber_sockbuf_ctrl(sb, LBER_SB_OPT_DATA_READY, NULL)) ok=1;
771 /* set up response chain */
774 new->lm_next = ld->ld_responses;
775 ld->ld_responses = new;
776 new->lm_chain_tail = new;
780 chain_head->lm_chain_tail = tmp;
783 /* "ok" means there's more to parse */
785 if (isv2) goto nextresp2;
788 /* got to end of datagram without a SearchResult. Free
789 * our dup'd ber, but leave any buffer alone. For v2 case,
790 * the previous response is still using this buffer. For v3,
791 * the new ber has no buffer to free yet.
796 } else if ( moremsgs ) {
797 /* got search result, and we had multiple responses in 1 datagram.
798 * stick the result onto the end of the chain, and then pull the
799 * first response off the head of the chain.
802 chain_head->lm_chain_tail = tmp;
803 *result = chkResponseList( ld, msgid, all );
804 ld->ld_errno = LDAP_SUCCESS;
805 return( (*result)->lm_msgtype );
810 /* is this the one we're looking for? */
811 if ( msgid == LDAP_RES_ANY || id == msgid ) {
812 if ( all == LDAP_MSG_ONE
813 || (new->lm_msgtype != LDAP_RES_SEARCH_RESULT
814 && new->lm_msgtype != LDAP_RES_SEARCH_ENTRY
815 && new->lm_msgtype != LDAP_RES_SEARCH_REFERENCE) ) {
817 ld->ld_errno = LDAP_SUCCESS;
819 } else if ( new->lm_msgtype == LDAP_RES_SEARCH_RESULT) {
820 foundit = 1; /* return the chain later */
825 * if not, we must add it to the list of responses. if
826 * the msgid is already there, it must be part of an existing
831 for ( l = ld->ld_responses; l != NULL; l = l->lm_next ) {
832 if ( l->lm_msgid == new->lm_msgid )
837 /* not part of an existing search response */
844 new->lm_next = ld->ld_responses;
845 ld->ld_responses = new;
846 new->lm_chain_tail = new;
850 Debug( LDAP_DEBUG_TRACE, "adding response id %ld type %ld:\n",
851 (long) new->lm_msgid, (long) new->lm_msgtype, 0 );
853 /* part of a search response - add to end of list of entries */
854 if (l->lm_chain == NULL) {
855 if ((l->lm_msgtype == LDAP_RES_SEARCH_ENTRY) ||
856 (l->lm_msgtype == LDAP_RES_SEARCH_REFERENCE) ||
857 (l->lm_msgtype == LDAP_RES_INTERMEDIATE)) {
858 /* do not advance lm_chain_tail in this case */
861 /*FIXME: ldap_msgfree( l );*/
863 l->lm_chain_tail = new;
866 if ((l->lm_chain_tail->lm_chain->lm_msgtype
867 == LDAP_RES_SEARCH_ENTRY) ||
868 (l->lm_chain_tail->lm_chain->lm_msgtype
869 == LDAP_RES_SEARCH_REFERENCE) ||
870 (l->lm_chain_tail->lm_chain->lm_msgtype
871 == LDAP_RES_INTERMEDIATE)) {
872 l->lm_chain_tail->lm_chain->lm_chain = new;
873 l->lm_chain_tail = l->lm_chain_tail->lm_chain;
875 /*FIXME: ldap_msgfree( l->lm_chain_tail->lm_chain );*/
876 l->lm_chain_tail->lm_chain = new;
880 /* return the whole chain if that's what we were looking for */
883 ld->ld_responses = l->lm_next;
885 prev->lm_next = l->lm_next;
891 ld->ld_errno = LDAP_SUCCESS;
894 if ( ber_sockbuf_ctrl( sb, LBER_SB_OPT_DATA_READY, NULL ) ) {
897 return( -2 ); /* continue looking */
902 build_result_ber( LDAP *ld, BerElement **bp, LDAPRequest *lr )
910 ber = ldap_alloc_ber_with_options( ld );
913 ld->ld_errno = LDAP_NO_MEMORY;
917 if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
918 lr->lr_res_msgtype, lr->lr_res_errno,
919 lr->lr_res_matched ? lr->lr_res_matched : "",
920 lr->lr_res_error ? lr->lr_res_error : "" ) == -1 ) {
922 ld->ld_errno = LDAP_ENCODING_ERROR;
924 return( LBER_ERROR );
929 if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) {
930 ld->ld_errno = LDAP_DECODING_ERROR;
932 return( LBER_ERROR );
935 if ( ber_get_int( ber, &along ) == LBER_ERROR ) {
936 ld->ld_errno = LDAP_DECODING_ERROR;
938 return( LBER_ERROR );
941 tag = ber_peek_tag( ber, &len );
943 if ( tag == LBER_ERROR ) {
944 ld->ld_errno = LDAP_DECODING_ERROR;
946 return( LBER_ERROR );
955 merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
958 * Merge error information in "lr" with "parentr" error code and string.
960 if ( lr->lr_res_errno == LDAP_PARTIAL_RESULTS ) {
961 parentr->lr_res_errno = lr->lr_res_errno;
962 if ( lr->lr_res_error != NULL ) {
963 (void)ldap_append_referral( ld, &parentr->lr_res_error,
966 } else if ( lr->lr_res_errno != LDAP_SUCCESS &&
967 parentr->lr_res_errno == LDAP_SUCCESS ) {
968 parentr->lr_res_errno = lr->lr_res_errno;
969 if ( parentr->lr_res_error != NULL ) {
970 LDAP_FREE( parentr->lr_res_error );
972 parentr->lr_res_error = lr->lr_res_error;
973 lr->lr_res_error = NULL;
974 if ( LDAP_NAME_ERROR( lr->lr_res_errno )) {
975 if ( parentr->lr_res_matched != NULL ) {
976 LDAP_FREE( parentr->lr_res_matched );
978 parentr->lr_res_matched = lr->lr_res_matched;
979 lr->lr_res_matched = NULL;
983 Debug( LDAP_DEBUG_TRACE, "merged parent (id %d) error info: ",
984 parentr->lr_msgid, 0, 0 );
985 Debug( LDAP_DEBUG_TRACE, "result errno %d, error <%s>, matched <%s>\n",
986 parentr->lr_res_errno, parentr->lr_res_error ?
987 parentr->lr_res_error : "", parentr->lr_res_matched ?
988 parentr->lr_res_matched : "" );
994 ldap_msgtype( LDAPMessage *lm )
996 assert( lm != NULL );
997 return ( lm != NULL ) ? lm->lm_msgtype : -1;
1002 ldap_msgid( LDAPMessage *lm )
1004 assert( lm != NULL );
1006 return ( lm != NULL ) ? lm->lm_msgid : -1;
1010 char * ldap_int_msgtype2str( ber_tag_t tag )
1013 case LDAP_RES_ADD: return "add";
1014 case LDAP_RES_BIND: return "bind";
1015 case LDAP_RES_COMPARE: return "compare";
1016 case LDAP_RES_DELETE: return "delete";
1017 case LDAP_RES_EXTENDED: return "extended-result";
1018 case LDAP_RES_INTERMEDIATE: return "intermediate";
1019 case LDAP_RES_MODIFY: return "modify";
1020 case LDAP_RES_RENAME: return "rename";
1021 case LDAP_RES_SEARCH_ENTRY: return "search-entry";
1022 case LDAP_RES_SEARCH_REFERENCE: return "search-reference";
1023 case LDAP_RES_SEARCH_RESULT: return "search-result";
1029 ldap_msgfree( LDAPMessage *lm )
1034 Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
1036 for ( ; lm != NULL; lm = next ) {
1037 next = lm->lm_chain;
1038 type = lm->lm_msgtype;
1039 ber_free( lm->lm_ber, 1 );
1040 LDAP_FREE( (char *) lm );
1047 * ldap_msgdelete - delete a message. It returns:
1048 * 0 if the entire message was deleted
1049 * -1 if the message was not found, or only part of it was found
1052 ldap_msgdelete( LDAP *ld, int msgid )
1054 LDAPMessage *lm, *prev;
1057 assert( ld != NULL );
1059 Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete\n", 0, 0, 0 );
1062 #ifdef LDAP_R_COMPILE
1063 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
1065 for ( lm = ld->ld_responses; lm != NULL; lm = lm->lm_next ) {
1066 if ( lm->lm_msgid == msgid )
1075 ld->ld_responses = lm->lm_next;
1077 prev->lm_next = lm->lm_next;
1079 #ifdef LDAP_R_COMPILE
1080 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
1082 if ( lm && ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY )
1090 * return 1 if message msgid is waiting to be abandoned, 0 otherwise
1093 ldap_abandoned( LDAP *ld, ber_int_t msgid )
1097 if ( ld->ld_abandoned == NULL )
1100 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
1101 if ( ld->ld_abandoned[i] == msgid )
1109 ldap_mark_abandoned( LDAP *ld, ber_int_t msgid )
1113 if ( ld->ld_abandoned == NULL )
1116 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
1117 if ( ld->ld_abandoned[i] == msgid )
1120 if ( ld->ld_abandoned[i] == -1 )
1123 for ( ; ld->ld_abandoned[i] != -1; i++ ) {
1124 ld->ld_abandoned[i] = ld->ld_abandoned[i + 1];