3 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1990 Regents of the University of Michigan.
11 * This notice applies to changes, created by or for Novell, Inc.,
12 * to preexisting works for which notices appear elsewhere in this file.
14 * Copyright (C) 1999, 2000 Novell, Inc. All Rights Reserved.
16 * THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND TREATIES.
17 * USE, MODIFICATION, AND REDISTRIBUTION OF THIS WORK IS SUBJECT TO VERSION
18 * 2.0.1 OF THE OPENLDAP PUBLIC LICENSE, A COPY OF WHICH IS AVAILABLE AT
19 * HTTP://WWW.OPENLDAP.ORG/LICENSE.HTML OR IN THE FILE "LICENSE" IN THE
20 * TOP-LEVEL DIRECTORY OF THE DISTRIBUTION. ANY USE OR EXPLOITATION OF THIS
21 * WORK OTHER THAN AS AUTHORIZED IN VERSION 2.0.1 OF THE OPENLDAP PUBLIC
22 * LICENSE, OR OTHER PRIOR WRITTEN CONSENT FROM NOVELL, COULD SUBJECT THE
23 * PERPETRATOR TO CRIMINAL AND CIVIL LIABILITY.
25 * Modification to OpenLDAP source by Novell, Inc.
26 * April 2000 sfs Add code to process V3 referrals and search results
28 * result.c - wait for an ldap result
33 * LDAPResult ::= SEQUENCE {
34 * resultCode ENUMERATED { ... },
36 * errorMessage LDAPString,
37 * referral Referral OPTIONAL
39 * Referral ::= SEQUENCE OF LDAPURL (one or more)
40 * LDAPURL ::= LDAPString (limited to URL chars)
47 #include <ac/stdlib.h>
50 #include <ac/socket.h>
51 #include <ac/string.h>
53 #include <ac/unistd.h>
58 static int ldap_abandoned LDAP_P(( LDAP *ld, ber_int_t msgid ));
59 static int ldap_mark_abandoned LDAP_P(( LDAP *ld, ber_int_t msgid ));
60 static int wait4msg LDAP_P(( LDAP *ld, ber_int_t msgid, int all, struct timeval *timeout,
61 LDAPMessage **result ));
62 static ber_tag_t try_read1msg LDAP_P(( LDAP *ld, ber_int_t msgid,
63 int all, Sockbuf *sb, LDAPConn *lc, LDAPMessage **result ));
64 static ber_tag_t build_result_ber LDAP_P(( LDAP *ld, BerElement **bp, LDAPRequest *lr ));
65 static void merge_error_info LDAP_P(( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr ));
66 static LDAPMessage * chkResponseList LDAP_P(( LDAP *ld, int msgid, int all));
70 * ldap_result - wait for an ldap result response to a message from the
71 * ldap server. If msgid is LDAP_RES_ANY (-1), any message will be
72 * accepted. If msgid is LDAP_RES_UNSOLICITED (0), any unsolicited
73 * message is accepted. Otherwise ldap_result will wait for a response
74 * with msgid. If all is LDAP_MSG_ONE (0) the first message with id
75 * msgid will be accepted, otherwise, ldap_result will wait for all
76 * responses with id msgid and then return a pointer to the entire list
77 * of messages. In general, this is only useful for search responses,
78 * which can be of three message types (zero or more entries, zero or
79 * search references, followed by an ldap result). An extension to
80 * LDAPv3 allows partial extended responses to be returned in response
81 * to any request. The type of the first message received is returned.
82 * When waiting, any messages that have been abandoned are discarded.
85 * ldap_result( s, msgid, all, timeout, result )
92 struct timeval *timeout,
93 LDAPMessage **result )
98 assert( result != NULL );
101 LDAP_LOG (( "result", LDAP_LEVEL_ARGS, "ldap_result msgid %d\n", msgid ));
103 Debug( LDAP_DEBUG_TRACE, "ldap_result msgid %d\n", msgid, 0, 0 );
110 if( result == NULL ) {
111 ld->ld_errno = LDAP_PARAM_ERROR;
115 lm = chkResponseList(ld, msgid, all);
118 return( wait4msg( ld, msgid, all, timeout, result ) );
122 ld->ld_errno = LDAP_SUCCESS;
123 return( lm->lm_msgtype );
132 LDAPMessage *lm, *lastlm, *nextlm;
134 * Look through the list of responses we have received on
135 * this association and see if the response we're interested in
136 * is there. If it is, return it. If not, call wait4msg() to
137 * wait until it arrives or timeout occurs.
141 LDAP_LOG (( "result", LDAP_LEVEL_ARGS,
142 "ldap_chkResponseList for msgid=%d, all=%d\n", msgid, all ));
144 Debug( LDAP_DEBUG_TRACE,
145 "ldap_chkResponseList for msgid=%d, all=%d\n",
149 for ( lm = ld->ld_responses; lm != NULL; lm = nextlm ) {
150 nextlm = lm->lm_next;
152 if ( ldap_abandoned( ld, lm->lm_msgid ) ) {
154 LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1,
155 "ldap_chkResponseList msg abandoned, msgid %d\n", msgid ));
157 Debug( LDAP_DEBUG_TRACE,
158 "ldap_chkResponseList msg abandoned, msgid %d\n",
161 ldap_mark_abandoned( ld, lm->lm_msgid );
163 if ( lastlm == NULL ) {
164 /* Remove first entry in list */
165 ld->ld_responses = lm->lm_next;
167 lastlm->lm_next = nextlm;
175 if ( msgid == LDAP_RES_ANY || lm->lm_msgid == msgid ) {
178 if ( all == LDAP_MSG_ONE || msgid == LDAP_RES_UNSOLICITED ) {
182 for ( tmp = lm; tmp != NULL; tmp = tmp->lm_chain ) {
183 if ( tmp->lm_msgtype != LDAP_RES_SEARCH_ENTRY
184 && tmp->lm_msgtype != LDAP_RES_SEARCH_REFERENCE
185 && tmp->lm_msgtype != LDAP_RES_EXTENDED_PARTIAL )
201 /* Found an entry, remove it from the list */
202 if ( lastlm == NULL ) {
203 ld->ld_responses = (all == LDAP_MSG_ONE && lm->lm_chain != NULL
204 ? lm->lm_chain : lm->lm_next);
206 lastlm->lm_next = (all == LDAP_MSG_ONE && lm->lm_chain != NULL
207 ? lm->lm_chain : lm->lm_next);
209 if ( all == LDAP_MSG_ONE && lm->lm_chain != NULL ) {
210 lm->lm_chain->lm_next = lm->lm_next;
219 LDAP_LOG (( "result", LDAP_LEVEL_RESULTS,
220 "ldap_chkResponseList returns NULL\n" ));
222 Debug( LDAP_DEBUG_TRACE,
223 "ldap_chkResponseList returns NULL\n", 0, 0, 0);
227 LDAP_LOG (( "result", LDAP_LEVEL_RESULTS,
228 "ldap_chkResponseList returns msgid %d, type 0x02lu\n",
229 lm->lm_msgid, (unsigned long) lm->lm_msgtype ));
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);
245 struct timeval *timeout,
246 LDAPMessage **result )
249 struct timeval tv, *tvp;
250 time_t start_time = 0;
252 LDAPConn *lc, *nextlc;
254 assert( ld != NULL );
255 assert( result != NULL );
258 if ( timeout == NULL ) {
260 LDAP_LOG (( "result", LDAP_LEVEL_ARGS,
261 "wait4msg (infinite timeout), msgid %d\n", msgid ));
263 Debug( LDAP_DEBUG_TRACE, "wait4msg (infinite timeout), msgid %d\n",
268 LDAP_LOG (( "result", LDAP_LEVEL_ARGS,
269 "wait4msg (timeout %ld sec, %ld usec), msgid %d\n",
270 (long) timeout->tv_sec, (long) timeout->tv_usec, msgid ));
272 Debug( LDAP_DEBUG_TRACE, "wait4msg (timeout %ld sec, %ld usec), msgid %d\n",
273 (long) timeout->tv_sec, (long) timeout->tv_usec, msgid );
276 #endif /* LDAP_DEBUG */
278 if ( timeout == NULL ) {
283 start_time = time( NULL );
290 LDAP_LOG (( "result", LDAP_LEVEL_ARGS,
291 "wait4msg continue, msgid %d, all %d\n", msgid, all ));
293 Debug( LDAP_DEBUG_TRACE, "wait4msg continue, msgid %d, all %d\n",
296 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
297 ldap_dump_connection( ld, ld->ld_conns, 1 );
298 ldap_dump_requests_and_responses( ld );
300 #endif /* LDAP_DEBUG */
302 if( (*result = chkResponseList(ld, msgid, all)) != NULL ) {
303 rc = (*result)->lm_msgtype;
306 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
307 if ( ber_sockbuf_ctrl( lc->lconn_sb,
308 LBER_SB_OPT_DATA_READY, NULL ) ) {
309 rc = try_read1msg( ld, msgid, all, lc->lconn_sb,
316 rc = ldap_int_select( ld, tvp );
322 LDAP_LOG (( "result", LDAP_LEVEL_ARGS,
323 "wait4msg: ldap_int_select returned -1: errno %d\n",
326 Debug( LDAP_DEBUG_TRACE,
327 "ldap_int_select returned -1: errno %d\n",
333 if ( rc == 0 || ( rc == -1 && (
334 !LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART)
335 || errno != EINTR )))
337 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
343 rc = -2; /* select interrupted: loop */
346 for ( lc = ld->ld_conns; rc == -2 && lc != NULL;
348 nextlc = lc->lconn_next;
349 if ( lc->lconn_status ==
350 LDAP_CONNST_CONNECTED &&
351 ldap_is_read_ready( ld,
353 rc = try_read1msg( ld, msgid, all,
354 lc->lconn_sb, lc, result );
361 if ( rc == -2 && tvp != NULL ) {
362 tmp_time = time( NULL );
363 if (( tv.tv_sec -= ( tmp_time - start_time )) <= 0 ) {
364 rc = 0; /* timed out */
365 ld->ld_errno = LDAP_TIMEOUT;
370 LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1,
371 "wait4msg: %ld secs to go\n", (long) tv.tv_sec ));
373 Debug( LDAP_DEBUG_TRACE, "wait4msg: %ld secs to go\n",
374 (long) tv.tv_sec, 0, 0 );
376 start_time = tmp_time;
391 LDAPMessage **result )
394 LDAPMessage *new, *l, *prev, *tmp;
399 LDAPRequest *lr, *tmplr;
401 int rc, refer_cnt, hadref, simple_request;
404 * v3ref = flag for V3 referral / search reference
405 * 0 = not a ref, 1 = sucessfully chased ref, -1 = pass ref to application
409 assert( ld != NULL );
410 assert( lc != NULL );
413 LDAP_LOG (( "result", LDAP_LEVEL_ARGS, "read1msg: msgid %d, all %d\n",
416 Debug( LDAP_DEBUG_TRACE, "read1msg: msgid %d, all %d\n", msgid, all, 0 );
419 if ( lc->lconn_ber == NULL ) {
420 lc->lconn_ber = ldap_alloc_ber_with_options(ld);
422 if( lc->lconn_ber == NULL ) {
428 assert( LBER_VALID (ber) );
430 /* get the next message */
432 #ifdef LDAP_CONNECTIONLESS
433 if ( LDAP_IS_UDP(ld) ) {
434 struct sockaddr from;
435 ber_int_sb_read(sb, &from, sizeof(struct sockaddr));
438 if ( (tag = ber_get_next( sb, &len, ber ))
439 != LDAP_TAG_MESSAGE ) {
440 if ( tag == LBER_DEFAULT) {
443 LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1,
444 "read1msg: ber_get_next failed\n" ));
446 Debug( LDAP_DEBUG_CONNS,
447 "ber_get_next failed.\n", 0, 0, 0 );
451 if (errno==EWOULDBLOCK) return -2;
454 if (errno == EAGAIN) return -2;
456 ld->ld_errno = LDAP_SERVER_DOWN;
459 ld->ld_errno = LDAP_LOCAL_ERROR;
464 * We read a complete message.
465 * The connection should no longer need this ber.
467 lc->lconn_ber = NULL;
470 if ( ber_get_int( ber, &id ) == LBER_ERROR ) {
472 ld->ld_errno = LDAP_DECODING_ERROR;
476 /* if it's been abandoned, toss it */
477 if ( ldap_abandoned( ld, id ) ) {
480 LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1,
481 "read1msg: abandoned\n" ));
483 Debug( LDAP_DEBUG_ANY, "abandoned\n", 0, 0, 0);
485 return( -2 ); /* continue looking */
488 if (( lr = ldap_find_request_by_msgid( ld, id )) == NULL ) {
490 LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1,
491 "read1msg: no request for response with msgid %ld (tossing)\n",
494 Debug( LDAP_DEBUG_ANY,
495 "no request for response with msgid %ld (tossing)\n",
499 return( -2 ); /* continue looking */
501 #ifdef LDAP_CONNECTIONLESS
502 if (LDAP_IS_UDP(ld) && ld->ld_options.ldo_version == LDAP_VERSION2) {
504 ber_scanf(ber, "m{", &blank);
507 /* the message type */
508 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
509 ld->ld_errno = LDAP_DECODING_ERROR;
515 LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1,
516 "read1msg: ldap_read: message type %s msgid %ld, original id %ld\n",
517 ldap_int_msgtype2str( tag ),
518 (long) lr->lr_msgid, (long) lr->lr_origid ));
520 Debug( LDAP_DEBUG_TRACE,
521 "ldap_read: message type %s msgid %ld, original id %ld\n",
522 ldap_int_msgtype2str( tag ),
523 (long) lr->lr_msgid, (long) lr->lr_origid );
528 hadref = simple_request = 0;
529 rc = -2; /* default is to keep looking (no response found) */
530 lr->lr_res_msgtype = tag;
533 * This code figures out if we are going to chase a
534 * referral / search reference, or pass it back to the application
536 v3ref = 0; /* Assume not a V3 search reference or referral */
537 if( (tag != LDAP_RES_SEARCH_ENTRY) && (ld->ld_version > LDAP_VERSION2) ) {
538 BerElement tmpber = *ber; /* struct copy */
541 if( tag == LDAP_RES_SEARCH_REFERENCE) {
542 /* This is a V3 search reference */
543 /* Assume we do not chase the reference, but pass it to application */
545 if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ||
546 (lr->lr_parent != NULL) )
548 /* Get the referral list */
549 if ( ber_scanf( &tmpber, "{v}", &refs ) == LBER_ERROR ) {
550 rc = LDAP_DECODING_ERROR;
552 /* Note: refs arrary is freed by ldap_chase_v3referrals */
553 refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
554 1, &lr->lr_res_error, &hadref );
555 if ( refer_cnt > 0 ) { /* sucessfully chased reference */
556 /* If haven't got end search, set chasing referrals */
557 if( lr->lr_status != LDAP_REQST_COMPLETED) {
558 lr->lr_status = LDAP_REQST_CHASINGREFS;
560 LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1,
561 "read1msg: search ref chased,"
562 "mark request chasing refs, id = %d\n",
565 Debug( LDAP_DEBUG_TRACE,
566 "read1msg: search ref chased, mark request chasing refs, id = %d\n",
570 v3ref = 1; /* We sucessfully chased the reference */
575 /* Check for V3 referral */
577 if ( ber_scanf( &tmpber, "{iaa",/*}*/ &lderr,
578 &lr->lr_res_matched, &lr->lr_res_error )
580 /* Check if V3 referral */
581 if( ber_peek_tag( &tmpber, &len) == LDAP_TAG_REFERRAL ) {
582 /* We have a V3 referral, assume we cannot chase it */
584 if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS)
585 || (lr->lr_parent != NULL) )
587 v3ref = -1; /* Assume referral not chased and return it to app */
588 /* Get the referral list */
589 if( ber_scanf( &tmpber, "{v}", &refs) == LBER_ERROR) {
590 rc = LDAP_DECODING_ERROR;
591 lr->lr_status = LDAP_REQST_COMPLETED;
593 LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1,
594 "read1msg: referral decode error,"
595 "mark request completed, id = %d\n",
598 Debug( LDAP_DEBUG_TRACE,
599 "read1msg: referral decode error, mark request completed, id = %d\n",
603 /* Chase the referral
604 * Note: refs arrary is freed by ldap_chase_v3referrals
606 refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
607 0, &lr->lr_res_error, &hadref );
608 lr->lr_status = LDAP_REQST_COMPLETED;
610 LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1,
611 "read1msg: referral chased,"
612 "mark request completed, id = %d\n",
615 Debug( LDAP_DEBUG_TRACE,
616 "read1msg: referral chased, mark request completed, id = %d\n",
620 v3ref = 1; /* Referral successfully chased */
626 if( lr->lr_res_matched != NULL ) {
627 LDAP_FREE( lr->lr_res_matched );
628 lr->lr_res_matched = NULL;
630 if( lr->lr_res_error != NULL ) {
631 LDAP_FREE( lr->lr_res_error );
632 lr->lr_res_error = NULL;
638 /* All results that just return a status, i.e. don't return data
639 * go through the following code. This code also chases V2 referrals
640 * and checks if all referrals have been chased.
642 if ( (tag != LDAP_RES_SEARCH_ENTRY) && (v3ref > -1) ) {
643 /* For a v3 search referral/reference, only come here if already chased it */
644 if ( ld->ld_version >= LDAP_VERSION2 &&
645 ( lr->lr_parent != NULL ||
646 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ) )
648 tmpber = *ber; /* struct copy */
650 ; /* V3 search reference or V3 referral sucessfully chased */
651 } else if ( ber_scanf( &tmpber, "{iaa}", &lderr,
652 &lr->lr_res_matched, &lr->lr_res_error )
654 if ( lderr != LDAP_SUCCESS ) {
655 /* referrals are in error string */
656 refer_cnt = ldap_chase_referrals( ld, lr,
657 &lr->lr_res_error, -1, &hadref );
658 lr->lr_status = LDAP_REQST_COMPLETED;
660 LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1,
661 "read1msg: V2 referral chased,"
662 "mark request completed, id = %d\n",
665 Debug( LDAP_DEBUG_TRACE,
666 "read1msg: V2 referral chased, mark request completed, id = %d\n", lr->lr_msgid, 0, 0);
670 /* save errno, message, and matched string */
671 if ( !hadref || lr->lr_res_error == NULL ) {
672 lr->lr_res_errno = ( lderr ==
673 LDAP_PARTIAL_RESULTS ) ? LDAP_SUCCESS
675 } else if ( ld->ld_errno != LDAP_SUCCESS ) {
676 lr->lr_res_errno = ld->ld_errno;
678 lr->lr_res_errno = LDAP_PARTIAL_RESULTS;
681 LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1,
682 "read1msg: new result: res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
683 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
684 lr->lr_res_matched ? lr->lr_res_matched : "" ));
686 Debug( LDAP_DEBUG_TRACE,
687 "new result: res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
688 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
689 lr->lr_res_matched ? lr->lr_res_matched : "" );
695 LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1,
696 "read1msg: %d new referrals\n", refer_cnt ));
698 Debug( LDAP_DEBUG_TRACE,
699 "read1msg: %d new referrals\n", refer_cnt, 0, 0 );
702 if ( refer_cnt != 0 ) { /* chasing referrals */
705 if ( refer_cnt < 0 ) {
706 return( -1 ); /* fatal error */
708 lr->lr_res_errno = LDAP_SUCCESS; /* sucessfully chased referral */
710 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
711 /* request without any referrals */
712 simple_request = ( hadref ? 0 : 1 );
714 /* request with referrals or child request */
719 lr->lr_status = LDAP_REQST_COMPLETED; /* declare this request done */
721 LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1,
722 "read1msg: mark request completed, id = %d\n", lr->lr_msgid ));
724 Debug( LDAP_DEBUG_TRACE,
725 "read1msg: mark request completed, id = %d\n", lr->lr_msgid, 0, 0);
727 while ( lr->lr_parent != NULL ) {
728 merge_error_info( ld, lr->lr_parent, lr );
731 if ( --lr->lr_outrefcnt > 0 ) {
732 break; /* not completely done yet */
736 /* Check if all requests are finished, lr is now parent */
738 if (tmplr->lr_status == LDAP_REQST_COMPLETED) {
739 for(tmplr=lr->lr_child; tmplr != NULL; tmplr=tmplr->lr_refnext) {
740 if( tmplr->lr_status != LDAP_REQST_COMPLETED) {
746 /* This is the parent request if the request has referrals */
747 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL && tmplr == NULL ) {
749 tag = lr->lr_res_msgtype;
751 LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1,
752 "read1msg: request %ld done\n", (long) id ));
753 LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1,
754 "read1msg: res_errno: %d,res_error: <%s>, res_matched: <%s>\n",
755 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
756 lr->lr_res_matched ? lr->lr_res_matched : "" ));
758 Debug( LDAP_DEBUG_ANY, "request %ld done\n",
760 Debug( LDAP_DEBUG_TRACE,
761 "res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
762 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
763 lr->lr_res_matched ? lr->lr_res_matched : "" );
765 if ( !simple_request ) {
768 if ( build_result_ber( ld, &ber, lr )
770 rc = -1; /* fatal error */
774 ldap_free_request( ld, lr );
778 ldap_free_connection( ld, lc, 0, 1 );
787 /* make a new ldap message */
788 if ( (new = (LDAPMessage *) LDAP_CALLOC( 1, sizeof(LDAPMessage) ))
790 ld->ld_errno = LDAP_NO_MEMORY;
793 new->lm_msgid = (int)id;
794 new->lm_msgtype = tag;
798 if ( ld->ld_cache != NULL ) {
799 ldap_add_result_to_cache( ld, new );
801 #endif /* LDAP_NOCACHE */
803 /* is this the one we're looking for? */
804 if ( msgid == LDAP_RES_ANY || id == msgid ) {
805 if ( all == LDAP_MSG_ONE
806 || (new->lm_msgtype != LDAP_RES_SEARCH_RESULT
807 && new->lm_msgtype != LDAP_RES_SEARCH_ENTRY
808 && new->lm_msgtype != LDAP_RES_SEARCH_REFERENCE) ) {
810 ld->ld_errno = LDAP_SUCCESS;
812 } else if ( new->lm_msgtype == LDAP_RES_SEARCH_RESULT) {
813 foundit = 1; /* return the chain later */
818 * if not, we must add it to the list of responses. if
819 * the msgid is already there, it must be part of an existing
824 for ( l = ld->ld_responses; l != NULL; l = l->lm_next ) {
825 if ( l->lm_msgid == new->lm_msgid )
830 /* not part of an existing search response */
834 ld->ld_errno = LDAP_SUCCESS;
838 new->lm_next = ld->ld_responses;
839 ld->ld_responses = new;
840 return( -2 ); /* continue looking */
844 LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1,
845 "read1msg: adding response id %ld type %ld\n",
846 (long) new->lm_msgid, (long) new->lm_msgtype ));
848 Debug( LDAP_DEBUG_TRACE, "adding response id %ld type %ld:\n",
849 (long) new->lm_msgid, (long) new->lm_msgtype, 0 );
852 /* part of a search response - add to end of list of entries */
853 for ( tmp = l; (tmp->lm_chain != NULL) &&
854 ((tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY) ||
855 (tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_REFERENCE) ||
856 (tmp->lm_chain->lm_msgtype == LDAP_RES_EXTENDED_PARTIAL ));
857 tmp = tmp->lm_chain )
861 /* return the whole chain if that's what we were looking for */
864 ld->ld_responses = l->lm_next;
866 prev->lm_next = l->lm_next;
868 ld->ld_errno = LDAP_SUCCESS;
869 #ifdef LDAP_WORLD_P16
871 * XXX questionable fix; see text for [P16] on
872 * http://www.critical-angle.com/ldapworld/patch/
874 * inclusion of this patch causes searchs to hang on
877 return( l->lm_msgtype );
878 #else /* LDAP_WORLD_P16 */
880 #endif /* !LDAP_WORLD_P16 */
883 return( -2 ); /* continue looking */
888 build_result_ber( LDAP *ld, BerElement **bp, LDAPRequest *lr )
896 ber = ldap_alloc_ber_with_options( ld );
899 ld->ld_errno = LDAP_NO_MEMORY;
903 if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
904 lr->lr_res_msgtype, lr->lr_res_errno,
905 lr->lr_res_matched ? lr->lr_res_matched : "",
906 lr->lr_res_error ? lr->lr_res_error : "" ) == -1 ) {
908 ld->ld_errno = LDAP_ENCODING_ERROR;
910 return( LBER_ERROR );
915 if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) {
916 ld->ld_errno = LDAP_DECODING_ERROR;
918 return( LBER_ERROR );
921 if ( ber_get_int( ber, &along ) == LBER_ERROR ) {
922 ld->ld_errno = LDAP_DECODING_ERROR;
924 return( LBER_ERROR );
927 tag = ber_peek_tag( ber, &len );
929 if ( tag == LBER_ERROR ) {
930 ld->ld_errno = LDAP_DECODING_ERROR;
932 return( LBER_ERROR );
941 merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
944 * Merge error information in "lr" with "parentr" error code and string.
946 if ( lr->lr_res_errno == LDAP_PARTIAL_RESULTS ) {
947 parentr->lr_res_errno = lr->lr_res_errno;
948 if ( lr->lr_res_error != NULL ) {
949 (void)ldap_append_referral( ld, &parentr->lr_res_error,
952 } else if ( lr->lr_res_errno != LDAP_SUCCESS &&
953 parentr->lr_res_errno == LDAP_SUCCESS ) {
954 parentr->lr_res_errno = lr->lr_res_errno;
955 if ( parentr->lr_res_error != NULL ) {
956 LDAP_FREE( parentr->lr_res_error );
958 parentr->lr_res_error = lr->lr_res_error;
959 lr->lr_res_error = NULL;
960 if ( LDAP_NAME_ERROR( lr->lr_res_errno )) {
961 if ( parentr->lr_res_matched != NULL ) {
962 LDAP_FREE( parentr->lr_res_matched );
964 parentr->lr_res_matched = lr->lr_res_matched;
965 lr->lr_res_matched = NULL;
970 LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1,
971 "read1msg: merged parent (id %d) error info: result errno %d, "
972 "error <%s>, matched <%s>\n", parentr->lr_msgid,
973 parentr->lr_res_errno, parentr->lr_res_error ?
974 parentr->lr_res_error : "", parentr->lr_res_matched ?
975 parentr->lr_res_matched : "" ));
977 Debug( LDAP_DEBUG_TRACE, "merged parent (id %d) error info: ",
978 parentr->lr_msgid, 0, 0 );
979 Debug( LDAP_DEBUG_TRACE, "result errno %d, error <%s>, matched <%s>\n",
980 parentr->lr_res_errno, parentr->lr_res_error ?
981 parentr->lr_res_error : "", parentr->lr_res_matched ?
982 parentr->lr_res_matched : "" );
989 ldap_msgtype( LDAPMessage *lm )
991 assert( lm != NULL );
992 return ( lm != NULL ) ? lm->lm_msgtype : -1;
997 ldap_msgid( LDAPMessage *lm )
999 assert( lm != NULL );
1001 return ( lm != NULL ) ? lm->lm_msgid : -1;
1005 char * ldap_int_msgtype2str( ber_tag_t tag )
1008 case LDAP_RES_ADD: return "add";
1009 case LDAP_RES_BIND: return "bind";
1010 case LDAP_RES_COMPARE: return "compare";
1011 case LDAP_RES_DELETE: return "delete";
1012 case LDAP_RES_EXTENDED: return "extended-result";
1013 case LDAP_RES_EXTENDED_PARTIAL: return "extended-partial";
1014 case LDAP_RES_MODIFY: return "modify";
1015 case LDAP_RES_RENAME: return "rename";
1016 case LDAP_RES_SEARCH_ENTRY: return "search-entry";
1017 case LDAP_RES_SEARCH_REFERENCE: return "search-reference";
1018 case LDAP_RES_SEARCH_RESULT: return "search-result";
1024 ldap_msgfree( LDAPMessage *lm )
1030 LDAP_LOG (( "result", LDAP_LEVEL_ENTRY, "ldap_msgfree\n" ));
1032 Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
1035 for ( ; lm != NULL; lm = next ) {
1036 next = lm->lm_chain;
1037 type = lm->lm_msgtype;
1038 ber_free( lm->lm_ber, 1 );
1039 LDAP_FREE( (char *) lm );
1046 * ldap_msgdelete - delete a message. It returns:
1047 * 0 if the entire message was deleted
1048 * -1 if the message was not found, or only part of it was found
1051 ldap_msgdelete( LDAP *ld, int msgid )
1053 LDAPMessage *lm, *prev;
1055 assert( ld != NULL );
1058 LDAP_LOG (( "result", LDAP_LEVEL_ENTRY, "ldap_msgdelete\n" ));
1060 Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete\n", 0, 0, 0 );
1064 for ( lm = ld->ld_responses; lm != NULL; lm = lm->lm_next ) {
1065 if ( lm->lm_msgid == msgid )
1074 ld->ld_responses = lm->lm_next;
1076 prev->lm_next = lm->lm_next;
1078 if ( ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY )
1086 * return 1 if message msgid is waiting to be abandoned, 0 otherwise
1089 ldap_abandoned( LDAP *ld, ber_int_t msgid )
1093 if ( ld->ld_abandoned == NULL )
1096 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
1097 if ( ld->ld_abandoned[i] == msgid )
1105 ldap_mark_abandoned( LDAP *ld, ber_int_t msgid )
1109 if ( ld->ld_abandoned == NULL )
1112 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
1113 if ( ld->ld_abandoned[i] == msgid )
1116 if ( ld->ld_abandoned[i] == -1 )
1119 for ( ; ld->ld_abandoned[i] != -1; i++ ) {
1120 ld->ld_abandoned[i] = ld->ld_abandoned[i + 1];