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 );
115 LDAP_LOG ( OPERATION, ARGS, "ldap_result msgid %d\n", msgid, 0, 0 );
117 Debug( LDAP_DEBUG_TRACE, "ldap_result msgid %d\n", msgid, 0, 0 );
120 #ifdef LDAP_R_COMPILE
121 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
123 lm = chkResponseList(ld, msgid, all);
126 rc = wait4msg( ld, msgid, all, timeout, result );
129 ld->ld_errno = LDAP_SUCCESS;
132 #ifdef LDAP_R_COMPILE
133 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
144 LDAPMessage *lm, *lastlm, *nextlm;
146 * Look through the list of responses we have received on
147 * this association and see if the response we're interested in
148 * is there. If it is, return it. If not, call wait4msg() to
149 * wait until it arrives or timeout occurs.
153 LDAP_LOG ( OPERATION, ARGS, "ldap_chkResponseList for msgid=%d, all=%d\n",
156 Debug( LDAP_DEBUG_TRACE,
157 "ldap_chkResponseList for msgid=%d, all=%d\n",
161 for ( lm = ld->ld_responses; lm != NULL; lm = nextlm ) {
162 nextlm = lm->lm_next;
164 if ( ldap_abandoned( ld, lm->lm_msgid ) ) {
166 LDAP_LOG ( OPERATION, DETAIL1,
167 "ldap_chkResponseList msg abandoned, msgid %d\n", msgid, 0, 0 );
169 Debug( LDAP_DEBUG_TRACE,
170 "ldap_chkResponseList msg abandoned, msgid %d\n",
173 ldap_mark_abandoned( ld, lm->lm_msgid );
175 if ( lastlm == NULL ) {
176 /* Remove first entry in list */
177 ld->ld_responses = lm->lm_next;
179 lastlm->lm_next = nextlm;
187 if ( msgid == LDAP_RES_ANY || lm->lm_msgid == msgid ) {
190 if ( all == LDAP_MSG_ONE || msgid == LDAP_RES_UNSOLICITED ) {
194 for ( tmp = lm; tmp != NULL; tmp = tmp->lm_chain ) {
195 if ( tmp->lm_msgtype != LDAP_RES_SEARCH_ENTRY
196 && tmp->lm_msgtype != LDAP_RES_SEARCH_REFERENCE
197 && tmp->lm_msgtype != LDAP_RES_INTERMEDIATE )
213 /* Found an entry, remove it from the list */
214 if ( lastlm == NULL ) {
215 ld->ld_responses = (all == LDAP_MSG_ONE && lm->lm_chain != NULL
216 ? lm->lm_chain : lm->lm_next);
218 lastlm->lm_next = (all == LDAP_MSG_ONE && lm->lm_chain != NULL
219 ? lm->lm_chain : lm->lm_next);
221 if ( all == LDAP_MSG_ONE && lm->lm_chain != NULL ) {
222 lm->lm_chain->lm_next = lm->lm_next;
231 LDAP_LOG ( OPERATION, RESULTS, "ldap_chkResponseList returns NULL\n",
234 Debug( LDAP_DEBUG_TRACE,
235 "ldap_chkResponseList returns NULL\n", 0, 0, 0);
239 LDAP_LOG ( OPERATION, RESULTS,
240 "ldap_chkResponseList returns msgid %d, type 0x%02lu\n",
241 lm->lm_msgid, (unsigned long) lm->lm_msgtype, 0 );
243 Debug( LDAP_DEBUG_TRACE,
244 "ldap_chkResponseList returns msgid %d, type 0x%02lu\n",
245 lm->lm_msgid, (unsigned long) lm->lm_msgtype, 0);
257 struct timeval *timeout,
258 LDAPMessage **result )
261 struct timeval tv, *tvp;
262 time_t start_time = 0;
264 LDAPConn *lc, *nextlc;
266 assert( ld != NULL );
267 assert( result != NULL );
270 if ( timeout == NULL ) {
272 LDAP_LOG ( OPERATION, ARGS,
273 "wait4msg (infinite timeout), msgid %d\n", msgid, 0, 0 );
275 Debug( LDAP_DEBUG_TRACE, "wait4msg (infinite timeout), msgid %d\n",
280 LDAP_LOG ( OPERATION, ARGS,
281 "wait4msg (timeout %ld sec, %ld usec), msgid %d\n",
282 (long) timeout->tv_sec, (long) timeout->tv_usec, msgid );
284 Debug( LDAP_DEBUG_TRACE, "wait4msg (timeout %ld sec, %ld usec), msgid %d\n",
285 (long) timeout->tv_sec, (long) timeout->tv_usec, msgid );
288 #endif /* LDAP_DEBUG */
290 if ( timeout == NULL ) {
295 start_time = time( NULL );
302 LDAP_LOG ( OPERATION, ARGS,
303 "wait4msg continue, msgid %d, all %d\n", msgid, all, 0 );
305 Debug( LDAP_DEBUG_TRACE, "wait4msg continue, msgid %d, all %d\n",
308 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
309 ldap_dump_connection( ld, ld->ld_conns, 1 );
310 ldap_dump_requests_and_responses( ld );
312 #endif /* LDAP_DEBUG */
314 if( (*result = chkResponseList(ld, msgid, all)) != NULL ) {
315 rc = (*result)->lm_msgtype;
319 for ( lc = ld->ld_conns; lc != NULL; lc = nextlc ) {
320 nextlc = lc->lconn_next;
321 if ( ber_sockbuf_ctrl( lc->lconn_sb,
322 LBER_SB_OPT_DATA_READY, NULL ) ) {
323 rc = try_read1msg( ld, msgid, all, lc->lconn_sb,
331 rc = ldap_int_select( ld, tvp );
335 LDAP_LOG ( OPERATION, ARGS,
336 "wait4msg: ldap_int_select returned -1: errno %d\n",
339 Debug( LDAP_DEBUG_TRACE,
340 "ldap_int_select returned -1: errno %d\n",
346 if ( rc == 0 || ( rc == -1 && (
347 !LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART)
348 || errno != EINTR )))
350 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
356 rc = -2; /* select interrupted: loop */
359 #ifdef LDAP_R_COMPILE
360 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
362 if ( ld->ld_requests &&
363 ld->ld_requests->lr_status == LDAP_REQST_WRITING &&
364 ldap_is_write_ready( ld,
365 ld->ld_requests->lr_conn->lconn_sb ) ) {
366 ldap_int_flush_request( ld, ld->ld_requests );
368 #ifdef LDAP_R_COMPILE
369 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
371 for ( lc = ld->ld_conns; rc == -2 && lc != NULL;
373 nextlc = lc->lconn_next;
374 if ( lc->lconn_status ==
375 LDAP_CONNST_CONNECTED &&
376 ldap_is_read_ready( ld,
378 rc = try_read1msg( ld, msgid, all,
379 lc->lconn_sb, &lc, result );
380 if ( lc == NULL ) lc = nextlc;
387 if ( rc == -2 && tvp != NULL ) {
388 tmp_time = time( NULL );
389 if (( tv.tv_sec -= ( tmp_time - start_time )) <= 0 ) {
390 rc = 0; /* timed out */
391 ld->ld_errno = LDAP_TIMEOUT;
396 LDAP_LOG ( OPERATION, DETAIL1,
397 "wait4msg: %ld secs to go\n", (long) tv.tv_sec, 0, 0 );
399 Debug( LDAP_DEBUG_TRACE, "wait4msg: %ld secs to go\n",
400 (long) tv.tv_sec, 0, 0 );
402 start_time = tmp_time;
417 LDAPMessage **result )
420 LDAPMessage *new, *l, *prev, *tmp;
425 LDAPRequest *lr, *tmplr;
428 int rc, refer_cnt, hadref, simple_request;
430 #ifdef LDAP_CONNECTIONLESS
431 int firstmsg = 1, moremsgs = 0, isv2 = 0;
434 * v3ref = flag for V3 referral / search reference
435 * 0 = not a ref, 1 = sucessfully chased ref, -1 = pass ref to application
439 assert( ld != NULL );
440 assert( lcp != NULL );
441 assert( *lcp != NULL );
444 LDAP_LOG ( OPERATION, ARGS, "read1msg: msgid %d, all %d\n", msgid, all, 0 );
446 Debug( LDAP_DEBUG_TRACE, "read1msg: msgid %d, all %d\n", msgid, all, 0 );
452 if ( lc->lconn_ber == NULL ) {
453 lc->lconn_ber = ldap_alloc_ber_with_options(ld);
455 if( lc->lconn_ber == NULL ) {
461 assert( LBER_VALID (ber) );
463 /* get the next message */
465 #ifdef LDAP_CONNECTIONLESS
466 if ( LDAP_IS_UDP(ld) ) {
467 struct sockaddr from;
468 ber_int_sb_read(sb, &from, sizeof(struct sockaddr));
469 if (ld->ld_options.ldo_version == LDAP_VERSION2) isv2=1;
473 tag = ber_get_next( sb, &len, ber );
474 if ( tag == LDAP_TAG_MESSAGE ) {
476 * We read a complete message.
477 * The connection should no longer need this ber.
479 lc->lconn_ber = NULL;
481 if ( tag != LDAP_TAG_MESSAGE ) {
482 if ( tag == LBER_DEFAULT) {
485 LDAP_LOG ( OPERATION, DETAIL1,
486 "read1msg: ber_get_next failed\n", 0, 0, 0 );
488 Debug( LDAP_DEBUG_CONNS,
489 "ber_get_next failed.\n", 0, 0, 0 );
493 if (errno==EWOULDBLOCK) return -2;
496 if (errno == EAGAIN) return -2;
498 ld->ld_errno = LDAP_SERVER_DOWN;
501 ld->ld_errno = LDAP_LOCAL_ERROR;
506 if ( ber_get_int( ber, &id ) == LBER_ERROR ) {
508 ld->ld_errno = LDAP_DECODING_ERROR;
512 /* if it's been abandoned, toss it */
513 if ( ldap_abandoned( ld, id ) ) {
515 LDAP_LOG ( OPERATION, DETAIL1, "read1msg: abandoned\n", 0, 0, 0 );
517 Debug( LDAP_DEBUG_ANY, "abandoned\n", 0, 0, 0);
521 if ( ber_sockbuf_ctrl( sb, LBER_SB_OPT_DATA_READY, NULL ) ) {
524 return( -2 ); /* continue looking */
527 if (( lr = ldap_find_request_by_msgid( ld, id )) == NULL ) {
529 LDAP_LOG ( OPERATION, DETAIL1,
530 "read1msg: no request for response with msgid %ld (tossing)\n",
533 Debug( LDAP_DEBUG_ANY,
534 "no request for response with msgid %ld (tossing)\n",
539 #ifdef LDAP_CONNECTIONLESS
540 if (LDAP_IS_UDP(ld) && isv2) {
541 ber_scanf(ber, "x{");
545 /* the message type */
546 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
547 ld->ld_errno = LDAP_DECODING_ERROR;
553 LDAP_LOG ( OPERATION, DETAIL1,
554 "read1msg: ldap_read: message type %s msgid %ld, original id %ld\n",
555 ldap_int_msgtype2str( tag ),
556 (long) lr->lr_msgid, (long) lr->lr_origid );
558 Debug( LDAP_DEBUG_TRACE,
559 "ldap_read: message type %s msgid %ld, original id %ld\n",
560 ldap_int_msgtype2str( tag ),
561 (long) lr->lr_msgid, (long) lr->lr_origid );
566 hadref = simple_request = 0;
567 rc = -2; /* default is to keep looking (no response found) */
568 lr->lr_res_msgtype = tag;
571 * This code figures out if we are going to chase a
572 * referral / search reference, or pass it back to the application
574 v3ref = 0; /* Assume not a V3 search reference or referral */
575 if( (tag != LDAP_RES_SEARCH_ENTRY) && (ld->ld_version > LDAP_VERSION2) ) {
576 BerElement tmpber = *ber; /* struct copy */
579 if( tag == LDAP_RES_SEARCH_REFERENCE) {
580 /* This is a V3 search reference */
581 /* Assume we do not chase the reference, but pass it to application */
583 if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ||
584 (lr->lr_parent != NULL) )
586 /* Get the referral list */
587 if ( ber_scanf( &tmpber, "{v}", &refs ) == LBER_ERROR ) {
588 rc = LDAP_DECODING_ERROR;
590 /* Note: refs arrary is freed by ldap_chase_v3referrals */
591 refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
592 1, &lr->lr_res_error, &hadref );
593 if ( refer_cnt > 0 ) { /* sucessfully chased reference */
594 /* If haven't got end search, set chasing referrals */
595 if( lr->lr_status != LDAP_REQST_COMPLETED) {
596 lr->lr_status = LDAP_REQST_CHASINGREFS;
598 LDAP_LOG ( OPERATION, DETAIL1,
599 "read1msg: search ref chased,"
600 "mark request chasing refs, id = %d\n",
601 lr->lr_msgid, 0, 0 );
603 Debug( LDAP_DEBUG_TRACE,
604 "read1msg: search ref chased, mark request chasing refs, id = %d\n",
608 v3ref = 1; /* We sucessfully chased the reference */
613 /* Check for V3 referral */
615 if ( ber_scanf( &tmpber, "{iaa",/*}*/ &lderr,
616 &lr->lr_res_matched, &lr->lr_res_error )
618 /* Check if V3 referral */
619 if( ber_peek_tag( &tmpber, &len) == LDAP_TAG_REFERRAL ) {
620 /* We have a V3 referral, assume we cannot chase it */
622 if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS)
623 || (lr->lr_parent != NULL) )
625 v3ref = -1; /* Assume referral not chased and return it to app */
626 /* Get the referral list */
627 if( ber_scanf( &tmpber, "{v}", &refs) == LBER_ERROR) {
628 rc = LDAP_DECODING_ERROR;
629 lr->lr_status = LDAP_REQST_COMPLETED;
631 LDAP_LOG ( OPERATION, DETAIL1,
632 "read1msg: referral decode error,"
633 "mark request completed, id = %d\n",
634 lr->lr_msgid, 0, 0 );
636 Debug( LDAP_DEBUG_TRACE,
637 "read1msg: referral decode error, mark request completed, id = %d\n",
641 /* Chase the referral
642 * Note: refs arrary is freed by ldap_chase_v3referrals
644 refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
645 0, &lr->lr_res_error, &hadref );
646 lr->lr_status = LDAP_REQST_COMPLETED;
648 LDAP_LOG ( OPERATION, DETAIL1,
649 "read1msg: referral chased,"
650 "mark request completed, id = %d\n",
651 lr->lr_msgid, 0, 0 );
653 Debug( LDAP_DEBUG_TRACE,
654 "read1msg: referral chased, mark request completed, id = %d\n",
658 v3ref = 1; /* Referral successfully chased */
664 if( lr->lr_res_matched != NULL ) {
665 LDAP_FREE( lr->lr_res_matched );
666 lr->lr_res_matched = NULL;
668 if( lr->lr_res_error != NULL ) {
669 LDAP_FREE( lr->lr_res_error );
670 lr->lr_res_error = NULL;
676 /* All results that just return a status, i.e. don't return data
677 * go through the following code. This code also chases V2 referrals
678 * and checks if all referrals have been chased.
680 if ( (tag != LDAP_RES_SEARCH_ENTRY) && (v3ref > -1) &&
681 (tag != LDAP_RES_INTERMEDIATE ))
683 /* For a v3 search referral/reference, only come here if already chased it */
684 if ( ld->ld_version >= LDAP_VERSION2 &&
685 ( lr->lr_parent != NULL ||
686 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ) )
688 tmpber = *ber; /* struct copy */
690 /* V3 search reference or V3 referral
691 * sucessfully chased. If this message
692 * is a search result, then it has no more
693 * outstanding referrals.
695 if ( tag == LDAP_RES_SEARCH_RESULT )
697 } else if ( ber_scanf( &tmpber, "{iaa}", &lderr,
698 &lr->lr_res_matched, &lr->lr_res_error )
700 if ( lderr != LDAP_SUCCESS ) {
701 /* referrals are in error string */
702 refer_cnt = ldap_chase_referrals( ld, lr,
703 &lr->lr_res_error, -1, &hadref );
704 lr->lr_status = LDAP_REQST_COMPLETED;
706 LDAP_LOG ( OPERATION, DETAIL1,
707 "read1msg: V2 referral chased,"
708 "mark request completed, id = %d\n",
709 lr->lr_msgid, 0, 0 );
711 Debug( LDAP_DEBUG_TRACE,
712 "read1msg: V2 referral chased, mark request completed, id = %d\n", lr->lr_msgid, 0, 0);
716 /* save errno, message, and matched string */
717 if ( !hadref || lr->lr_res_error == NULL ) {
718 lr->lr_res_errno = ( lderr ==
719 LDAP_PARTIAL_RESULTS ) ? LDAP_SUCCESS
721 } else if ( ld->ld_errno != LDAP_SUCCESS ) {
722 lr->lr_res_errno = ld->ld_errno;
724 lr->lr_res_errno = LDAP_PARTIAL_RESULTS;
727 LDAP_LOG ( OPERATION, DETAIL1,
728 "read1msg: new result: res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
729 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
730 lr->lr_res_matched ? lr->lr_res_matched : "" );
732 Debug( LDAP_DEBUG_TRACE,
733 "new result: res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
734 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
735 lr->lr_res_matched ? lr->lr_res_matched : "" );
741 LDAP_LOG ( OPERATION, DETAIL1, "read1msg: %d new referrals\n",
744 Debug( LDAP_DEBUG_TRACE,
745 "read1msg: %d new referrals\n", refer_cnt, 0, 0 );
748 if ( refer_cnt != 0 ) { /* chasing referrals */
751 if ( refer_cnt < 0 ) {
752 return( -1 ); /* fatal error */
754 lr->lr_res_errno = LDAP_SUCCESS; /* sucessfully chased referral */
756 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
757 /* request without any referrals */
758 simple_request = ( hadref ? 0 : 1 );
760 /* request with referrals or child request */
765 lr->lr_status = LDAP_REQST_COMPLETED; /* declare this request done */
767 LDAP_LOG ( OPERATION, DETAIL1,
768 "read1msg: mark request completed, id = %d\n",
769 lr->lr_msgid, 0, 0 );
771 Debug( LDAP_DEBUG_TRACE,
772 "read1msg: mark request completed, id = %d\n", lr->lr_msgid, 0, 0);
774 while ( lr->lr_parent != NULL ) {
775 merge_error_info( ld, lr->lr_parent, lr );
778 if ( --lr->lr_outrefcnt > 0 ) {
779 break; /* not completely done yet */
783 /* Check if all requests are finished, lr is now parent */
785 if (tmplr->lr_status == LDAP_REQST_COMPLETED) {
786 for(tmplr=lr->lr_child; tmplr != NULL; tmplr=tmplr->lr_refnext) {
787 if( tmplr->lr_status != LDAP_REQST_COMPLETED) {
793 /* This is the parent request if the request has referrals */
794 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL && tmplr == NULL ) {
796 tag = lr->lr_res_msgtype;
798 LDAP_LOG ( OPERATION, DETAIL1,
799 "read1msg: request %ld done\n", (long) id, 0, 0 );
800 LDAP_LOG ( OPERATION, DETAIL1,
801 "read1msg: 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 : "" );
805 Debug( LDAP_DEBUG_ANY, "request %ld done\n",
807 Debug( LDAP_DEBUG_TRACE,
808 "res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
809 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
810 lr->lr_res_matched ? lr->lr_res_matched : "" );
812 if ( !simple_request ) {
815 if ( build_result_ber( ld, &ber, lr )
817 rc = -1; /* fatal error */
821 ldap_free_request( ld, lr );
825 ldap_free_connection( ld, lc, 0, 1 );
835 /* make a new ldap message */
836 if ( (new = (LDAPMessage *) LDAP_CALLOC( 1, sizeof(LDAPMessage) ))
838 ld->ld_errno = LDAP_NO_MEMORY;
841 new->lm_msgid = (int)id;
842 new->lm_msgtype = tag;
845 #ifdef LDAP_CONNECTIONLESS
846 /* CLDAP replies all fit in a single datagram. In LDAPv2 RFC1798
847 * the responses are all a sequence wrapped in one message. In
848 * LDAPv3 each response is in its own message. The datagram must
849 * end with a SearchResult. We can't just parse each response in
850 * separate calls to try_read1msg because the header info is only
851 * present at the beginning of the datagram, not at the beginning
852 * of each response. So parse all the responses at once and queue
853 * them up, then pull off the first response to return to the
854 * caller when all parsing is complete.
856 if ( LDAP_IS_UDP(ld) ) {
857 /* If not a result, look for more */
858 if ( tag != LDAP_RES_SEARCH_RESULT ) {
862 /* LDAPv2: dup the current ber, skip past the current
863 * response, and see if there are any more after it.
865 ber = ber_dup( ber );
866 ber_scanf( ber, "x" );
867 if (ber_peek_tag(ber, &len) != LBER_DEFAULT) {
868 /* There's more - dup the ber buffer so they can all be
869 * individually freed by ldap_msgfree.
872 ber_get_option(ber, LBER_OPT_BER_REMAINING_BYTES, &len);
873 bv.bv_val = LDAP_MALLOC(len);
876 ber_read(ber, bv.bv_val, len);
878 ber_init2(ber, &bv, ld->ld_lberoptions );
882 /* LDAPv3: Just allocate a new ber. Since this is a buffered
883 * datagram, if the sockbuf is readable we still have data
886 ber = ldap_alloc_ber_with_options(ld);
887 if (ber_sockbuf_ctrl(sb, LBER_SB_OPT_DATA_READY, NULL)) ok=1;
889 /* set up response chain */
892 new->lm_next = ld->ld_responses;
893 ld->ld_responses = new;
898 /* "ok" means there's more to parse */
900 if (isv2) goto nextresp2;
903 /* got to end of datagram without a SearchResult. Free
904 * our dup'd ber, but leave any buffer alone. For v2 case,
905 * the previous response is still using this buffer. For v3,
906 * the new ber has no buffer to free yet.
911 } else if ( moremsgs ) {
912 /* got search result, and we had multiple responses in 1 datagram.
913 * stick the result onto the end of the chain, and then pull the
914 * first response off the head of the chain.
917 *result = chkResponseList( ld, msgid, all );
918 ld->ld_errno = LDAP_SUCCESS;
919 return( (*result)->lm_msgtype );
924 /* is this the one we're looking for? */
925 if ( msgid == LDAP_RES_ANY || id == msgid ) {
926 if ( all == LDAP_MSG_ONE
927 || (new->lm_msgtype != LDAP_RES_SEARCH_RESULT
928 && new->lm_msgtype != LDAP_RES_SEARCH_ENTRY
929 && new->lm_msgtype != LDAP_RES_SEARCH_REFERENCE) ) {
931 ld->ld_errno = LDAP_SUCCESS;
933 } else if ( new->lm_msgtype == LDAP_RES_SEARCH_RESULT) {
934 foundit = 1; /* return the chain later */
939 * if not, we must add it to the list of responses. if
940 * the msgid is already there, it must be part of an existing
945 for ( l = ld->ld_responses; l != NULL; l = l->lm_next ) {
946 if ( l->lm_msgid == new->lm_msgid )
951 /* not part of an existing search response */
958 new->lm_next = ld->ld_responses;
959 ld->ld_responses = new;
964 LDAP_LOG ( OPERATION, DETAIL1,
965 "read1msg: adding response id %ld type %ld\n",
966 (long) new->lm_msgid, (long) new->lm_msgtype, 0 );
968 Debug( LDAP_DEBUG_TRACE, "adding response id %ld type %ld:\n",
969 (long) new->lm_msgid, (long) new->lm_msgtype, 0 );
972 /* part of a search response - add to end of list of entries */
973 for ( tmp = l; (tmp->lm_chain != NULL) &&
974 ((tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY) ||
975 (tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_REFERENCE) ||
976 (tmp->lm_chain->lm_msgtype == LDAP_RES_INTERMEDIATE ));
977 tmp = tmp->lm_chain )
981 /* return the whole chain if that's what we were looking for */
984 ld->ld_responses = l->lm_next;
986 prev->lm_next = l->lm_next;
992 ld->ld_errno = LDAP_SUCCESS;
995 if ( ber_sockbuf_ctrl( sb, LBER_SB_OPT_DATA_READY, NULL ) ) {
998 return( -2 ); /* continue looking */
1003 build_result_ber( LDAP *ld, BerElement **bp, LDAPRequest *lr )
1011 ber = ldap_alloc_ber_with_options( ld );
1014 ld->ld_errno = LDAP_NO_MEMORY;
1018 if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
1019 lr->lr_res_msgtype, lr->lr_res_errno,
1020 lr->lr_res_matched ? lr->lr_res_matched : "",
1021 lr->lr_res_error ? lr->lr_res_error : "" ) == -1 ) {
1023 ld->ld_errno = LDAP_ENCODING_ERROR;
1025 return( LBER_ERROR );
1028 ber_reset( ber, 1 );
1030 if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) {
1031 ld->ld_errno = LDAP_DECODING_ERROR;
1033 return( LBER_ERROR );
1036 if ( ber_get_int( ber, &along ) == LBER_ERROR ) {
1037 ld->ld_errno = LDAP_DECODING_ERROR;
1039 return( LBER_ERROR );
1042 tag = ber_peek_tag( ber, &len );
1044 if ( tag == LBER_ERROR ) {
1045 ld->ld_errno = LDAP_DECODING_ERROR;
1047 return( LBER_ERROR );
1056 merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
1059 * Merge error information in "lr" with "parentr" error code and string.
1061 if ( lr->lr_res_errno == LDAP_PARTIAL_RESULTS ) {
1062 parentr->lr_res_errno = lr->lr_res_errno;
1063 if ( lr->lr_res_error != NULL ) {
1064 (void)ldap_append_referral( ld, &parentr->lr_res_error,
1067 } else if ( lr->lr_res_errno != LDAP_SUCCESS &&
1068 parentr->lr_res_errno == LDAP_SUCCESS ) {
1069 parentr->lr_res_errno = lr->lr_res_errno;
1070 if ( parentr->lr_res_error != NULL ) {
1071 LDAP_FREE( parentr->lr_res_error );
1073 parentr->lr_res_error = lr->lr_res_error;
1074 lr->lr_res_error = NULL;
1075 if ( LDAP_NAME_ERROR( lr->lr_res_errno )) {
1076 if ( parentr->lr_res_matched != NULL ) {
1077 LDAP_FREE( parentr->lr_res_matched );
1079 parentr->lr_res_matched = lr->lr_res_matched;
1080 lr->lr_res_matched = NULL;
1085 LDAP_LOG( OPERATION, DETAIL1, "merged parent (id %d) error info: ",
1086 parentr->lr_msgid, 0, 0 );
1087 LDAP_LOG( OPERATION, DETAIL1, "result errno %d, error <%s>, matched <%s>\n",
1088 parentr->lr_res_errno, parentr->lr_res_error ?
1089 parentr->lr_res_error : "", parentr->lr_res_matched ?
1090 parentr->lr_res_matched : "" );
1092 Debug( LDAP_DEBUG_TRACE, "merged parent (id %d) error info: ",
1093 parentr->lr_msgid, 0, 0 );
1094 Debug( LDAP_DEBUG_TRACE, "result errno %d, error <%s>, matched <%s>\n",
1095 parentr->lr_res_errno, parentr->lr_res_error ?
1096 parentr->lr_res_error : "", parentr->lr_res_matched ?
1097 parentr->lr_res_matched : "" );
1104 ldap_msgtype( LDAPMessage *lm )
1106 assert( lm != NULL );
1107 return ( lm != NULL ) ? lm->lm_msgtype : -1;
1112 ldap_msgid( LDAPMessage *lm )
1114 assert( lm != NULL );
1116 return ( lm != NULL ) ? lm->lm_msgid : -1;
1120 char * ldap_int_msgtype2str( ber_tag_t tag )
1123 case LDAP_RES_ADD: return "add";
1124 case LDAP_RES_BIND: return "bind";
1125 case LDAP_RES_COMPARE: return "compare";
1126 case LDAP_RES_DELETE: return "delete";
1127 case LDAP_RES_EXTENDED: return "extended-result";
1128 case LDAP_RES_INTERMEDIATE: return "intermediate";
1129 case LDAP_RES_MODIFY: return "modify";
1130 case LDAP_RES_RENAME: return "rename";
1131 case LDAP_RES_SEARCH_ENTRY: return "search-entry";
1132 case LDAP_RES_SEARCH_REFERENCE: return "search-reference";
1133 case LDAP_RES_SEARCH_RESULT: return "search-result";
1139 ldap_msgfree( LDAPMessage *lm )
1145 LDAP_LOG ( OPERATION, ENTRY, "ldap_msgfree\n", 0, 0, 0 );
1147 Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
1150 for ( ; lm != NULL; lm = next ) {
1151 next = lm->lm_chain;
1152 type = lm->lm_msgtype;
1153 ber_free( lm->lm_ber, 1 );
1154 LDAP_FREE( (char *) lm );
1161 * ldap_msgdelete - delete a message. It returns:
1162 * 0 if the entire message was deleted
1163 * -1 if the message was not found, or only part of it was found
1166 ldap_msgdelete( LDAP *ld, int msgid )
1168 LDAPMessage *lm, *prev;
1171 assert( ld != NULL );
1174 LDAP_LOG ( OPERATION, ENTRY, "ldap_msgdelete\n", 0, 0, 0 );
1176 Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete\n", 0, 0, 0 );
1180 #ifdef LDAP_R_COMPILE
1181 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
1183 for ( lm = ld->ld_responses; lm != NULL; lm = lm->lm_next ) {
1184 if ( lm->lm_msgid == msgid )
1193 ld->ld_responses = lm->lm_next;
1195 prev->lm_next = lm->lm_next;
1197 #ifdef LDAP_R_COMPILE
1198 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
1200 if ( lm && ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY )
1208 * return 1 if message msgid is waiting to be abandoned, 0 otherwise
1211 ldap_abandoned( LDAP *ld, ber_int_t msgid )
1215 if ( ld->ld_abandoned == NULL )
1218 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
1219 if ( ld->ld_abandoned[i] == msgid )
1227 ldap_mark_abandoned( LDAP *ld, ber_int_t msgid )
1231 if ( ld->ld_abandoned == NULL )
1234 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
1235 if ( ld->ld_abandoned[i] == msgid )
1238 if ( ld->ld_abandoned[i] == -1 )
1241 for ( ; ld->ld_abandoned[i] != -1; i++ ) {
1242 ld->ld_abandoned[i] = ld->ld_abandoned[i + 1];