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;
318 for ( lc = ld->ld_conns; lc != NULL; lc = nextlc ) {
319 nextlc = lc->lconn_next;
320 if ( ber_sockbuf_ctrl( lc->lconn_sb,
321 LBER_SB_OPT_DATA_READY, NULL ) ) {
322 rc = try_read1msg( ld, msgid, all, lc->lconn_sb,
329 rc = ldap_int_select( ld, tvp );
333 LDAP_LOG ( OPERATION, ARGS,
334 "wait4msg: ldap_int_select returned -1: errno %d\n",
337 Debug( LDAP_DEBUG_TRACE,
338 "ldap_int_select returned -1: errno %d\n",
344 if ( rc == 0 || ( rc == -1 && (
345 !LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART)
346 || errno != EINTR )))
348 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
354 rc = -2; /* select interrupted: loop */
357 #ifdef LDAP_R_COMPILE
358 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
360 if ( ld->ld_requests &&
361 ld->ld_requests->lr_status == LDAP_REQST_WRITING &&
362 ldap_is_write_ready( ld,
363 ld->ld_requests->lr_conn->lconn_sb ) ) {
364 ldap_int_flush_request( ld, ld->ld_requests );
366 #ifdef LDAP_R_COMPILE
367 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
369 for ( lc = ld->ld_conns; rc == -2 && lc != NULL;
371 nextlc = lc->lconn_next;
372 if ( lc->lconn_status ==
373 LDAP_CONNST_CONNECTED &&
374 ldap_is_read_ready( ld,
376 rc = try_read1msg( ld, msgid, all,
377 lc->lconn_sb, &lc, result );
378 if ( lc == NULL ) lc = nextlc;
385 if ( rc == -2 && tvp != NULL ) {
386 tmp_time = time( NULL );
387 if (( tv.tv_sec -= ( tmp_time - start_time )) <= 0 ) {
388 rc = 0; /* timed out */
389 ld->ld_errno = LDAP_TIMEOUT;
394 LDAP_LOG ( OPERATION, DETAIL1,
395 "wait4msg: %ld secs to go\n", (long) tv.tv_sec, 0, 0 );
397 Debug( LDAP_DEBUG_TRACE, "wait4msg: %ld secs to go\n",
398 (long) tv.tv_sec, 0, 0 );
400 start_time = tmp_time;
415 LDAPMessage **result )
418 LDAPMessage *new, *l, *prev, *tmp;
423 LDAPRequest *lr, *tmplr;
426 int rc, refer_cnt, hadref, simple_request;
428 #ifdef LDAP_CONNECTIONLESS
429 int firstmsg = 1, moremsgs = 0, isv2 = 0;
432 * v3ref = flag for V3 referral / search reference
433 * 0 = not a ref, 1 = sucessfully chased ref, -1 = pass ref to application
437 assert( ld != NULL );
438 assert( lcp != NULL );
439 assert( *lcp != NULL );
442 LDAP_LOG ( OPERATION, ARGS, "read1msg: msgid %d, all %d\n", msgid, all, 0 );
444 Debug( LDAP_DEBUG_TRACE, "read1msg: msgid %d, all %d\n", msgid, all, 0 );
450 if ( lc->lconn_ber == NULL ) {
451 lc->lconn_ber = ldap_alloc_ber_with_options(ld);
453 if( lc->lconn_ber == NULL ) {
459 assert( LBER_VALID (ber) );
461 /* get the next message */
463 #ifdef LDAP_CONNECTIONLESS
464 if ( LDAP_IS_UDP(ld) ) {
465 struct sockaddr from;
466 ber_int_sb_read(sb, &from, sizeof(struct sockaddr));
467 if (ld->ld_options.ldo_version == LDAP_VERSION2) isv2=1;
471 tag = ber_get_next( sb, &len, ber );
472 if ( tag == LDAP_TAG_MESSAGE ) {
474 * We read a complete message.
475 * The connection should no longer need this ber.
477 lc->lconn_ber = NULL;
479 if ( tag != LDAP_TAG_MESSAGE ) {
480 if ( tag == LBER_DEFAULT) {
483 LDAP_LOG ( OPERATION, DETAIL1,
484 "read1msg: ber_get_next failed\n", 0, 0, 0 );
486 Debug( LDAP_DEBUG_CONNS,
487 "ber_get_next failed.\n", 0, 0, 0 );
491 if (errno==EWOULDBLOCK) return -2;
494 if (errno == EAGAIN) return -2;
496 ld->ld_errno = LDAP_SERVER_DOWN;
499 ld->ld_errno = LDAP_LOCAL_ERROR;
504 if ( ber_get_int( ber, &id ) == LBER_ERROR ) {
506 ld->ld_errno = LDAP_DECODING_ERROR;
510 /* if it's been abandoned, toss it */
511 if ( ldap_abandoned( ld, id ) ) {
513 LDAP_LOG ( OPERATION, DETAIL1, "read1msg: abandoned\n", 0, 0, 0 );
515 Debug( LDAP_DEBUG_ANY, "abandoned\n", 0, 0, 0);
519 if ( ber_sockbuf_ctrl( sb, LBER_SB_OPT_DATA_READY, NULL ) ) {
522 return( -2 ); /* continue looking */
525 if (( lr = ldap_find_request_by_msgid( ld, id )) == NULL ) {
527 LDAP_LOG ( OPERATION, DETAIL1,
528 "read1msg: no request for response with msgid %ld (tossing)\n",
531 Debug( LDAP_DEBUG_ANY,
532 "no request for response with msgid %ld (tossing)\n",
537 #ifdef LDAP_CONNECTIONLESS
538 if (LDAP_IS_UDP(ld) && isv2) {
539 ber_scanf(ber, "x{");
543 /* the message type */
544 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
545 ld->ld_errno = LDAP_DECODING_ERROR;
551 LDAP_LOG ( OPERATION, DETAIL1,
552 "read1msg: ldap_read: message type %s msgid %ld, original id %ld\n",
553 ldap_int_msgtype2str( tag ),
554 (long) lr->lr_msgid, (long) lr->lr_origid );
556 Debug( LDAP_DEBUG_TRACE,
557 "ldap_read: message type %s msgid %ld, original id %ld\n",
558 ldap_int_msgtype2str( tag ),
559 (long) lr->lr_msgid, (long) lr->lr_origid );
564 hadref = simple_request = 0;
565 rc = -2; /* default is to keep looking (no response found) */
566 lr->lr_res_msgtype = tag;
569 * This code figures out if we are going to chase a
570 * referral / search reference, or pass it back to the application
572 v3ref = 0; /* Assume not a V3 search reference or referral */
573 if( (tag != LDAP_RES_SEARCH_ENTRY) && (ld->ld_version > LDAP_VERSION2) ) {
574 BerElement tmpber = *ber; /* struct copy */
577 if( tag == LDAP_RES_SEARCH_REFERENCE) {
578 /* This is a V3 search reference */
579 /* Assume we do not chase the reference, but pass it to application */
581 if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ||
582 (lr->lr_parent != NULL) )
584 /* Get the referral list */
585 if ( ber_scanf( &tmpber, "{v}", &refs ) == LBER_ERROR ) {
586 rc = LDAP_DECODING_ERROR;
588 /* Note: refs arrary is freed by ldap_chase_v3referrals */
589 refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
590 1, &lr->lr_res_error, &hadref );
591 if ( refer_cnt > 0 ) { /* sucessfully chased reference */
592 /* If haven't got end search, set chasing referrals */
593 if( lr->lr_status != LDAP_REQST_COMPLETED) {
594 lr->lr_status = LDAP_REQST_CHASINGREFS;
596 LDAP_LOG ( OPERATION, DETAIL1,
597 "read1msg: search ref chased,"
598 "mark request chasing refs, id = %d\n",
599 lr->lr_msgid, 0, 0 );
601 Debug( LDAP_DEBUG_TRACE,
602 "read1msg: search ref chased, mark request chasing refs, id = %d\n",
606 v3ref = 1; /* We sucessfully chased the reference */
611 /* Check for V3 referral */
613 if ( ber_scanf( &tmpber, "{iaa",/*}*/ &lderr,
614 &lr->lr_res_matched, &lr->lr_res_error )
616 /* Check if V3 referral */
617 if( ber_peek_tag( &tmpber, &len) == LDAP_TAG_REFERRAL ) {
618 /* We have a V3 referral, assume we cannot chase it */
620 if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS)
621 || (lr->lr_parent != NULL) )
623 v3ref = -1; /* Assume referral not chased and return it to app */
624 /* Get the referral list */
625 if( ber_scanf( &tmpber, "{v}", &refs) == LBER_ERROR) {
626 rc = LDAP_DECODING_ERROR;
627 lr->lr_status = LDAP_REQST_COMPLETED;
629 LDAP_LOG ( OPERATION, DETAIL1,
630 "read1msg: referral decode error,"
631 "mark request completed, id = %d\n",
632 lr->lr_msgid, 0, 0 );
634 Debug( LDAP_DEBUG_TRACE,
635 "read1msg: referral decode error, mark request completed, id = %d\n",
639 /* Chase the referral
640 * Note: refs arrary is freed by ldap_chase_v3referrals
642 refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
643 0, &lr->lr_res_error, &hadref );
644 lr->lr_status = LDAP_REQST_COMPLETED;
646 LDAP_LOG ( OPERATION, DETAIL1,
647 "read1msg: referral chased,"
648 "mark request completed, id = %d\n",
649 lr->lr_msgid, 0, 0 );
651 Debug( LDAP_DEBUG_TRACE,
652 "read1msg: referral chased, mark request completed, id = %d\n",
656 v3ref = 1; /* Referral successfully chased */
662 if( lr->lr_res_matched != NULL ) {
663 LDAP_FREE( lr->lr_res_matched );
664 lr->lr_res_matched = NULL;
666 if( lr->lr_res_error != NULL ) {
667 LDAP_FREE( lr->lr_res_error );
668 lr->lr_res_error = NULL;
674 /* All results that just return a status, i.e. don't return data
675 * go through the following code. This code also chases V2 referrals
676 * and checks if all referrals have been chased.
678 if ( (tag != LDAP_RES_SEARCH_ENTRY) && (v3ref > -1) &&
679 (tag != LDAP_RES_INTERMEDIATE ))
681 /* For a v3 search referral/reference, only come here if already chased it */
682 if ( ld->ld_version >= LDAP_VERSION2 &&
683 ( lr->lr_parent != NULL ||
684 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ) )
686 tmpber = *ber; /* struct copy */
688 /* V3 search reference or V3 referral
689 * sucessfully chased. If this message
690 * is a search result, then it has no more
691 * outstanding referrals.
693 if ( tag == LDAP_RES_SEARCH_RESULT )
695 } else if ( ber_scanf( &tmpber, "{iaa}", &lderr,
696 &lr->lr_res_matched, &lr->lr_res_error )
698 if ( lderr != LDAP_SUCCESS ) {
699 /* referrals are in error string */
700 refer_cnt = ldap_chase_referrals( ld, lr,
701 &lr->lr_res_error, -1, &hadref );
702 lr->lr_status = LDAP_REQST_COMPLETED;
704 LDAP_LOG ( OPERATION, DETAIL1,
705 "read1msg: V2 referral chased,"
706 "mark request completed, id = %d\n",
707 lr->lr_msgid, 0, 0 );
709 Debug( LDAP_DEBUG_TRACE,
710 "read1msg: V2 referral chased, mark request completed, id = %d\n", lr->lr_msgid, 0, 0);
714 /* save errno, message, and matched string */
715 if ( !hadref || lr->lr_res_error == NULL ) {
716 lr->lr_res_errno = ( lderr ==
717 LDAP_PARTIAL_RESULTS ) ? LDAP_SUCCESS
719 } else if ( ld->ld_errno != LDAP_SUCCESS ) {
720 lr->lr_res_errno = ld->ld_errno;
722 lr->lr_res_errno = LDAP_PARTIAL_RESULTS;
725 LDAP_LOG ( OPERATION, DETAIL1,
726 "read1msg: new result: res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
727 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
728 lr->lr_res_matched ? lr->lr_res_matched : "" );
730 Debug( LDAP_DEBUG_TRACE,
731 "new result: res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
732 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
733 lr->lr_res_matched ? lr->lr_res_matched : "" );
739 LDAP_LOG ( OPERATION, DETAIL1, "read1msg: %d new referrals\n",
742 Debug( LDAP_DEBUG_TRACE,
743 "read1msg: %d new referrals\n", refer_cnt, 0, 0 );
746 if ( refer_cnt != 0 ) { /* chasing referrals */
749 if ( refer_cnt < 0 ) {
750 return( -1 ); /* fatal error */
752 lr->lr_res_errno = LDAP_SUCCESS; /* sucessfully chased referral */
754 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
755 /* request without any referrals */
756 simple_request = ( hadref ? 0 : 1 );
758 /* request with referrals or child request */
763 lr->lr_status = LDAP_REQST_COMPLETED; /* declare this request done */
765 LDAP_LOG ( OPERATION, DETAIL1,
766 "read1msg: mark request completed, id = %d\n",
767 lr->lr_msgid, 0, 0 );
769 Debug( LDAP_DEBUG_TRACE,
770 "read1msg: mark request completed, id = %d\n", lr->lr_msgid, 0, 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; tmplr != NULL; tmplr=tmplr->lr_refnext) {
785 if( tmplr->lr_status != LDAP_REQST_COMPLETED) {
791 /* This is the parent request if the request has referrals */
792 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL && tmplr == NULL ) {
794 tag = lr->lr_res_msgtype;
796 LDAP_LOG ( OPERATION, DETAIL1,
797 "read1msg: request %ld done\n", (long) id, 0, 0 );
798 LDAP_LOG ( OPERATION, DETAIL1,
799 "read1msg: res_errno: %d,res_error: <%s>, res_matched: <%s>\n",
800 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
801 lr->lr_res_matched ? lr->lr_res_matched : "" );
803 Debug( LDAP_DEBUG_ANY, "request %ld done\n",
805 Debug( LDAP_DEBUG_TRACE,
806 "res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
807 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
808 lr->lr_res_matched ? lr->lr_res_matched : "" );
810 if ( !simple_request ) {
813 if ( build_result_ber( ld, &ber, lr )
815 rc = -1; /* fatal error */
819 ldap_free_request( ld, lr );
823 ldap_free_connection( ld, lc, 0, 1 );
833 /* make a new ldap message */
834 if ( (new = (LDAPMessage *) LDAP_CALLOC( 1, sizeof(LDAPMessage) ))
836 ld->ld_errno = LDAP_NO_MEMORY;
839 new->lm_msgid = (int)id;
840 new->lm_msgtype = tag;
843 #ifdef LDAP_CONNECTIONLESS
844 /* CLDAP replies all fit in a single datagram. In LDAPv2 RFC1798
845 * the responses are all a sequence wrapped in one message. In
846 * LDAPv3 each response is in its own message. The datagram must
847 * end with a SearchResult. We can't just parse each response in
848 * separate calls to try_read1msg because the header info is only
849 * present at the beginning of the datagram, not at the beginning
850 * of each response. So parse all the responses at once and queue
851 * them up, then pull off the first response to return to the
852 * caller when all parsing is complete.
854 if ( LDAP_IS_UDP(ld) ) {
855 /* If not a result, look for more */
856 if ( tag != LDAP_RES_SEARCH_RESULT ) {
860 /* LDAPv2: dup the current ber, skip past the current
861 * response, and see if there are any more after it.
863 ber = ber_dup( ber );
864 ber_scanf( ber, "x" );
865 if (ber_peek_tag(ber, &len) != LBER_DEFAULT) {
866 /* There's more - dup the ber buffer so they can all be
867 * individually freed by ldap_msgfree.
870 ber_get_option(ber, LBER_OPT_BER_REMAINING_BYTES, &len);
871 bv.bv_val = LDAP_MALLOC(len);
874 ber_read(ber, bv.bv_val, len);
876 ber_init2(ber, &bv, ld->ld_lberoptions );
880 /* LDAPv3: Just allocate a new ber. Since this is a buffered
881 * datagram, if the sockbuf is readable we still have data
884 ber = ldap_alloc_ber_with_options(ld);
885 if (ber_sockbuf_ctrl(sb, LBER_SB_OPT_DATA_READY, NULL)) ok=1;
887 /* set up response chain */
890 new->lm_next = ld->ld_responses;
891 ld->ld_responses = new;
896 /* "ok" means there's more to parse */
898 if (isv2) goto nextresp2;
901 /* got to end of datagram without a SearchResult. Free
902 * our dup'd ber, but leave any buffer alone. For v2 case,
903 * the previous response is still using this buffer. For v3,
904 * the new ber has no buffer to free yet.
909 } else if ( moremsgs ) {
910 /* got search result, and we had multiple responses in 1 datagram.
911 * stick the result onto the end of the chain, and then pull the
912 * first response off the head of the chain.
915 *result = chkResponseList( ld, msgid, all );
916 ld->ld_errno = LDAP_SUCCESS;
917 return( (*result)->lm_msgtype );
922 /* is this the one we're looking for? */
923 if ( msgid == LDAP_RES_ANY || id == msgid ) {
924 if ( all == LDAP_MSG_ONE
925 || (new->lm_msgtype != LDAP_RES_SEARCH_RESULT
926 && new->lm_msgtype != LDAP_RES_SEARCH_ENTRY
927 && new->lm_msgtype != LDAP_RES_SEARCH_REFERENCE) ) {
929 ld->ld_errno = LDAP_SUCCESS;
931 } else if ( new->lm_msgtype == LDAP_RES_SEARCH_RESULT) {
932 foundit = 1; /* return the chain later */
937 * if not, we must add it to the list of responses. if
938 * the msgid is already there, it must be part of an existing
943 for ( l = ld->ld_responses; l != NULL; l = l->lm_next ) {
944 if ( l->lm_msgid == new->lm_msgid )
949 /* not part of an existing search response */
956 new->lm_next = ld->ld_responses;
957 ld->ld_responses = new;
962 LDAP_LOG ( OPERATION, DETAIL1,
963 "read1msg: adding response id %ld type %ld\n",
964 (long) new->lm_msgid, (long) new->lm_msgtype, 0 );
966 Debug( LDAP_DEBUG_TRACE, "adding response id %ld type %ld:\n",
967 (long) new->lm_msgid, (long) new->lm_msgtype, 0 );
970 /* part of a search response - add to end of list of entries */
971 for ( tmp = l; (tmp->lm_chain != NULL) &&
972 ((tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY) ||
973 (tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_REFERENCE) ||
974 (tmp->lm_chain->lm_msgtype == LDAP_RES_INTERMEDIATE ));
975 tmp = tmp->lm_chain )
979 /* return the whole chain if that's what we were looking for */
982 ld->ld_responses = l->lm_next;
984 prev->lm_next = l->lm_next;
990 ld->ld_errno = LDAP_SUCCESS;
993 if ( ber_sockbuf_ctrl( sb, LBER_SB_OPT_DATA_READY, NULL ) ) {
996 return( -2 ); /* continue looking */
1001 build_result_ber( LDAP *ld, BerElement **bp, LDAPRequest *lr )
1009 ber = ldap_alloc_ber_with_options( ld );
1012 ld->ld_errno = LDAP_NO_MEMORY;
1016 if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
1017 lr->lr_res_msgtype, lr->lr_res_errno,
1018 lr->lr_res_matched ? lr->lr_res_matched : "",
1019 lr->lr_res_error ? lr->lr_res_error : "" ) == -1 ) {
1021 ld->ld_errno = LDAP_ENCODING_ERROR;
1023 return( LBER_ERROR );
1026 ber_reset( ber, 1 );
1028 if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) {
1029 ld->ld_errno = LDAP_DECODING_ERROR;
1031 return( LBER_ERROR );
1034 if ( ber_get_int( ber, &along ) == LBER_ERROR ) {
1035 ld->ld_errno = LDAP_DECODING_ERROR;
1037 return( LBER_ERROR );
1040 tag = ber_peek_tag( ber, &len );
1042 if ( tag == LBER_ERROR ) {
1043 ld->ld_errno = LDAP_DECODING_ERROR;
1045 return( LBER_ERROR );
1054 merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
1057 * Merge error information in "lr" with "parentr" error code and string.
1059 if ( lr->lr_res_errno == LDAP_PARTIAL_RESULTS ) {
1060 parentr->lr_res_errno = lr->lr_res_errno;
1061 if ( lr->lr_res_error != NULL ) {
1062 (void)ldap_append_referral( ld, &parentr->lr_res_error,
1065 } else if ( lr->lr_res_errno != LDAP_SUCCESS &&
1066 parentr->lr_res_errno == LDAP_SUCCESS ) {
1067 parentr->lr_res_errno = lr->lr_res_errno;
1068 if ( parentr->lr_res_error != NULL ) {
1069 LDAP_FREE( parentr->lr_res_error );
1071 parentr->lr_res_error = lr->lr_res_error;
1072 lr->lr_res_error = NULL;
1073 if ( LDAP_NAME_ERROR( lr->lr_res_errno )) {
1074 if ( parentr->lr_res_matched != NULL ) {
1075 LDAP_FREE( parentr->lr_res_matched );
1077 parentr->lr_res_matched = lr->lr_res_matched;
1078 lr->lr_res_matched = NULL;
1083 LDAP_LOG( OPERATION, DETAIL1, "merged parent (id %d) error info: ",
1084 parentr->lr_msgid, 0, 0 );
1085 LDAP_LOG( OPERATION, DETAIL1, "result errno %d, error <%s>, matched <%s>\n",
1086 parentr->lr_res_errno, parentr->lr_res_error ?
1087 parentr->lr_res_error : "", parentr->lr_res_matched ?
1088 parentr->lr_res_matched : "" );
1090 Debug( LDAP_DEBUG_TRACE, "merged parent (id %d) error info: ",
1091 parentr->lr_msgid, 0, 0 );
1092 Debug( LDAP_DEBUG_TRACE, "result errno %d, error <%s>, matched <%s>\n",
1093 parentr->lr_res_errno, parentr->lr_res_error ?
1094 parentr->lr_res_error : "", parentr->lr_res_matched ?
1095 parentr->lr_res_matched : "" );
1102 ldap_msgtype( LDAPMessage *lm )
1104 assert( lm != NULL );
1105 return ( lm != NULL ) ? lm->lm_msgtype : -1;
1110 ldap_msgid( LDAPMessage *lm )
1112 assert( lm != NULL );
1114 return ( lm != NULL ) ? lm->lm_msgid : -1;
1118 char * ldap_int_msgtype2str( ber_tag_t tag )
1121 case LDAP_RES_ADD: return "add";
1122 case LDAP_RES_BIND: return "bind";
1123 case LDAP_RES_COMPARE: return "compare";
1124 case LDAP_RES_DELETE: return "delete";
1125 case LDAP_RES_EXTENDED: return "extended-result";
1126 case LDAP_RES_INTERMEDIATE: return "intermediate";
1127 case LDAP_RES_MODIFY: return "modify";
1128 case LDAP_RES_RENAME: return "rename";
1129 case LDAP_RES_SEARCH_ENTRY: return "search-entry";
1130 case LDAP_RES_SEARCH_REFERENCE: return "search-reference";
1131 case LDAP_RES_SEARCH_RESULT: return "search-result";
1137 ldap_msgfree( LDAPMessage *lm )
1143 LDAP_LOG ( OPERATION, ENTRY, "ldap_msgfree\n", 0, 0, 0 );
1145 Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
1148 for ( ; lm != NULL; lm = next ) {
1149 next = lm->lm_chain;
1150 type = lm->lm_msgtype;
1151 ber_free( lm->lm_ber, 1 );
1152 LDAP_FREE( (char *) lm );
1159 * ldap_msgdelete - delete a message. It returns:
1160 * 0 if the entire message was deleted
1161 * -1 if the message was not found, or only part of it was found
1164 ldap_msgdelete( LDAP *ld, int msgid )
1166 LDAPMessage *lm, *prev;
1169 assert( ld != NULL );
1172 LDAP_LOG ( OPERATION, ENTRY, "ldap_msgdelete\n", 0, 0, 0 );
1174 Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete\n", 0, 0, 0 );
1178 #ifdef LDAP_R_COMPILE
1179 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
1181 for ( lm = ld->ld_responses; lm != NULL; lm = lm->lm_next ) {
1182 if ( lm->lm_msgid == msgid )
1191 ld->ld_responses = lm->lm_next;
1193 prev->lm_next = lm->lm_next;
1195 #ifdef LDAP_R_COMPILE
1196 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
1198 if ( lm && ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY )
1206 * return 1 if message msgid is waiting to be abandoned, 0 otherwise
1209 ldap_abandoned( LDAP *ld, ber_int_t msgid )
1213 if ( ld->ld_abandoned == NULL )
1216 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
1217 if ( ld->ld_abandoned[i] == msgid )
1225 ldap_mark_abandoned( LDAP *ld, ber_int_t msgid )
1229 if ( ld->ld_abandoned == NULL )
1232 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
1233 if ( ld->ld_abandoned[i] == msgid )
1236 if ( ld->ld_abandoned[i] == -1 )
1239 for ( ; ld->ld_abandoned[i] != -1; i++ ) {
1240 ld->ld_abandoned[i] = ld->ld_abandoned[i + 1];