1 /* result.c - wait for an ldap result */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
17 * All rights reserved.
19 /* This notice applies to changes, created by or for Novell, Inc.,
20 * to preexisting works for which notices appear elsewhere in this file.
22 * Copyright (C) 1999, 2000 Novell, Inc. All Rights Reserved.
24 * THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND TREATIES.
25 * USE, MODIFICATION, AND REDISTRIBUTION OF THIS WORK IS SUBJECT TO VERSION
26 * 2.0.1 OF THE OPENLDAP PUBLIC LICENSE, A COPY OF WHICH IS AVAILABLE AT
27 * HTTP://WWW.OPENLDAP.ORG/LICENSE.HTML OR IN THE FILE "LICENSE" IN THE
28 * TOP-LEVEL DIRECTORY OF THE DISTRIBUTION. ANY USE OR EXPLOITATION OF THIS
29 * WORK OTHER THAN AS AUTHORIZED IN VERSION 2.0.1 OF THE OPENLDAP PUBLIC
30 * LICENSE, OR OTHER PRIOR WRITTEN CONSENT FROM NOVELL, COULD SUBJECT THE
31 * PERPETRATOR TO CRIMINAL AND CIVIL LIABILITY.
33 * Modification to OpenLDAP source by Novell, Inc.
34 * April 2000 sfs Add code to process V3 referrals and search results
36 * Note: A verbatim copy of version 2.0.1 of the OpenLDAP Public License
37 * can be found in the file "build/LICENSE-2.0.1" in this distribution
38 * of OpenLDAP Software.
40 /* Portions Copyright (C) The Internet Society (1997)
41 * ASN.1 fragments are from RFC 2251; see RFC for full legal notices.
46 * LDAPResult ::= SEQUENCE {
47 * resultCode ENUMERATED { ... },
49 * errorMessage LDAPString,
50 * referral Referral OPTIONAL
52 * Referral ::= SEQUENCE OF LDAPURL (one or more)
53 * LDAPURL ::= LDAPString (limited to URL chars)
60 #include <ac/stdlib.h>
63 #include <ac/socket.h>
64 #include <ac/string.h>
66 #include <ac/unistd.h>
71 static int ldap_abandoned LDAP_P(( LDAP *ld, ber_int_t msgid ));
72 static int ldap_mark_abandoned LDAP_P(( LDAP *ld, ber_int_t msgid ));
73 static int wait4msg LDAP_P(( LDAP *ld, ber_int_t msgid, int all, struct timeval *timeout,
74 LDAPMessage **result ));
75 static ber_tag_t try_read1msg LDAP_P(( LDAP *ld, ber_int_t msgid,
76 int all, Sockbuf *sb, LDAPConn **lc, LDAPMessage **result ));
77 static ber_tag_t build_result_ber LDAP_P(( LDAP *ld, BerElement **bp, LDAPRequest *lr ));
78 static void merge_error_info LDAP_P(( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr ));
79 static LDAPMessage * chkResponseList LDAP_P(( LDAP *ld, int msgid, int all));
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 assert(lm->lm_chain_tail == lm);
182 if ((lm->lm_msgtype == LDAP_RES_SEARCH_ENTRY) ||
183 (lm->lm_msgtype == LDAP_RES_SEARCH_REFERENCE) ||
184 (lm->lm_msgtype == LDAP_RES_INTERMEDIATE)) {
190 assert(lm->lm_chain_tail != NULL);
191 assert(lm->lm_chain_tail->lm_chain != NULL);
192 if ((lm->lm_chain_tail->lm_chain->lm_msgtype
193 == LDAP_RES_SEARCH_ENTRY) ||
194 (lm->lm_chain_tail->lm_chain->lm_msgtype
195 == LDAP_RES_SEARCH_REFERENCE) ||
196 (lm->lm_chain_tail->lm_chain->lm_msgtype
197 == LDAP_RES_INTERMEDIATE)) {
200 tmp = lm->lm_chain_tail->lm_chain;
214 /* Found an entry, remove it from the list */
215 if ( lastlm == NULL ) {
216 ld->ld_responses = (all == LDAP_MSG_ONE && lm->lm_chain != NULL
217 ? lm->lm_chain : lm->lm_next);
219 lastlm->lm_next = (all == LDAP_MSG_ONE && lm->lm_chain != NULL
220 ? lm->lm_chain : lm->lm_next);
222 if ( all == LDAP_MSG_ONE && lm->lm_chain != NULL ) {
223 lm->lm_chain->lm_next = lm->lm_next;
224 lm->lm_chain->lm_chain_tail = ( lm->lm_chain_tail != lm ) ? lm->lm_chain_tail : lm->lm_chain;
225 assert(lm->lm_chain->lm_chain_tail != NULL);
227 lm->lm_chain_tail = NULL;
234 Debug( LDAP_DEBUG_TRACE,
235 "ldap_chkResponseList returns NULL\n", 0, 0, 0);
237 Debug( LDAP_DEBUG_TRACE,
238 "ldap_chkResponseList returns msgid %d, type 0x%02lu\n",
239 lm->lm_msgid, (unsigned long) lm->lm_msgtype, 0);
250 struct timeval *timeout,
251 LDAPMessage **result )
254 struct timeval tv, tv0, *tvp;
255 time_t start_time = 0;
257 LDAPConn *lc, *nextlc;
259 assert( ld != NULL );
260 assert( result != NULL );
263 if ( timeout == NULL ) {
264 Debug( LDAP_DEBUG_TRACE, "wait4msg (infinite timeout), msgid %d\n",
267 Debug( LDAP_DEBUG_TRACE, "wait4msg (timeout %ld sec, %ld usec), msgid %d\n",
268 (long) timeout->tv_sec, (long) timeout->tv_usec, msgid );
270 #endif /* LDAP_DEBUG */
272 if ( timeout == NULL ) {
278 start_time = time( NULL );
284 Debug( LDAP_DEBUG_TRACE, "wait4msg continue, msgid %d, all %d\n",
286 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
287 ldap_dump_connection( ld, ld->ld_conns, 1 );
288 ldap_dump_requests_and_responses( ld );
290 #endif /* LDAP_DEBUG */
292 if ( (*result = chkResponseList(ld, msgid, all)) != NULL ) {
293 rc = (*result)->lm_msgtype;
298 for ( lc = ld->ld_conns; lc != NULL; lc = nextlc ) {
299 nextlc = lc->lconn_next;
300 if ( ber_sockbuf_ctrl( lc->lconn_sb,
301 LBER_SB_OPT_DATA_READY, NULL ) ) {
302 rc = try_read1msg( ld, msgid, all, lc->lconn_sb,
310 rc = ldap_int_select( ld, tvp );
313 Debug( LDAP_DEBUG_TRACE,
314 "ldap_int_select returned -1: errno %d\n",
319 if ( rc == 0 || ( rc == -1 && (
320 !LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART)
321 || errno != EINTR )))
323 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
329 rc = -2; /* select interrupted: loop */
332 #ifdef LDAP_R_COMPILE
333 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
335 if ( ld->ld_requests &&
336 ld->ld_requests->lr_status == LDAP_REQST_WRITING &&
337 ldap_is_write_ready( ld,
338 ld->ld_requests->lr_conn->lconn_sb ) )
340 ldap_int_flush_request( ld, ld->ld_requests );
342 #ifdef LDAP_R_COMPILE
343 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
345 for ( lc = ld->ld_conns; rc == -2 && lc != NULL;
348 nextlc = lc->lconn_next;
349 if ( lc->lconn_status == LDAP_CONNST_CONNECTED &&
350 ldap_is_read_ready( ld, lc->lconn_sb ))
352 rc = try_read1msg( ld, msgid, all,
353 lc->lconn_sb, &lc, result );
354 if ( lc == NULL ) lc = nextlc;
361 if ( rc == -2 && tvp != NULL ) {
362 tmp_time = time( NULL );
363 if (( tv0.tv_sec -= ( tmp_time - start_time )) <= 0 ) {
364 rc = 0; /* timed out */
365 ld->ld_errno = LDAP_TIMEOUT;
368 tv.tv_sec = tv0.tv_sec;
370 Debug( LDAP_DEBUG_TRACE, "wait4msg: %ld secs to go\n",
371 (long) tv.tv_sec, 0, 0 );
372 start_time = tmp_time;
387 LDAPMessage **result )
390 LDAPMessage *newmsg, *l, *prev;
395 LDAPRequest *lr, *tmplr;
398 int rc, refer_cnt, hadref, simple_request;
401 #ifdef LDAP_CONNECTIONLESS
402 LDAPMessage *tmp = NULL, *chain_head = NULL;
403 int firstmsg = 1, moremsgs = 0, isv2 = 0;
407 * v3ref = flag for V3 referral / search reference
408 * 0 = not a ref, 1 = sucessfully chased ref, -1 = pass ref to application
412 assert( ld != NULL );
413 assert( lcp != NULL );
414 assert( *lcp != NULL );
416 Debug( LDAP_DEBUG_TRACE, "read1msg: msgid %d, all %d\n", msgid, all, 0 );
421 if ( lc->lconn_ber == NULL ) {
422 lc->lconn_ber = ldap_alloc_ber_with_options(ld);
424 if( lc->lconn_ber == NULL ) {
430 assert( LBER_VALID (ber) );
432 /* get the next message */
434 #ifdef LDAP_CONNECTIONLESS
435 if ( LDAP_IS_UDP(ld) ) {
436 struct sockaddr from;
437 ber_int_sb_read(sb, &from, sizeof(struct sockaddr));
438 if (ld->ld_options.ldo_version == LDAP_VERSION2) isv2=1;
442 tag = ber_get_next( sb, &len, ber );
443 if ( tag == LDAP_TAG_MESSAGE ) {
445 * We read a complete message.
446 * The connection should no longer need this ber.
448 lc->lconn_ber = NULL;
450 if ( tag != LDAP_TAG_MESSAGE ) {
451 if ( tag == LBER_DEFAULT) {
453 Debug( LDAP_DEBUG_CONNS,
454 "ber_get_next failed.\n", 0, 0, 0 );
457 if (errno==EWOULDBLOCK) return -2;
460 if (errno == EAGAIN) return -2;
462 ld->ld_errno = LDAP_SERVER_DOWN;
465 ld->ld_errno = LDAP_LOCAL_ERROR;
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 ) ) {
478 Debug( LDAP_DEBUG_ANY, "abandoned\n", 0, 0, 0);
481 if ( ber_sockbuf_ctrl( sb, LBER_SB_OPT_DATA_READY, NULL ) ) {
484 return( -2 ); /* continue looking */
487 lr = ldap_find_request_by_msgid( ld, id );
489 Debug( LDAP_DEBUG_ANY,
490 "no request for response with msgid %ld (tossing)\n",
494 #ifdef LDAP_CONNECTIONLESS
495 if (LDAP_IS_UDP(ld) && isv2) {
496 ber_scanf(ber, "x{");
500 /* the message type */
501 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
502 ld->ld_errno = LDAP_DECODING_ERROR;
507 Debug( LDAP_DEBUG_TRACE,
508 "ldap_read: message type %s msgid %ld, original id %ld\n",
509 ldap_int_msgtype2str( tag ),
510 (long) lr->lr_msgid, (long) lr->lr_origid );
514 hadref = simple_request = 0;
515 rc = -2; /* default is to keep looking (no response found) */
516 lr->lr_res_msgtype = tag;
519 * This code figures out if we are going to chase a
520 * referral / search reference, or pass it back to the application
522 v3ref = 0; /* Assume not a V3 search reference or referral */
523 if( (tag != LDAP_RES_SEARCH_ENTRY) && (ld->ld_version > LDAP_VERSION2) ) {
524 BerElement tmpber = *ber; /* struct copy */
527 if( tag == LDAP_RES_SEARCH_REFERENCE) {
528 /* This is a V3 search reference */
529 /* Assume we do not chase the reference, but pass it to application */
531 if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ||
532 (lr->lr_parent != NULL) )
534 /* Get the referral list */
535 if ( ber_scanf( &tmpber, "{v}", &refs ) == LBER_ERROR ) {
536 rc = LDAP_DECODING_ERROR;
538 /* Note: refs arrary is freed by ldap_chase_v3referrals */
539 refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
540 1, &lr->lr_res_error, &hadref );
541 if ( refer_cnt > 0 ) { /* sucessfully chased reference */
542 /* If haven't got end search, set chasing referrals */
543 if( lr->lr_status != LDAP_REQST_COMPLETED) {
544 lr->lr_status = LDAP_REQST_CHASINGREFS;
545 Debug( LDAP_DEBUG_TRACE,
546 "read1msg: search ref chased, mark request chasing refs, id = %d\n",
549 v3ref = 1; /* We sucessfully chased the reference */
554 /* Check for V3 referral */
556 char *lr_res_error = NULL;
558 if ( ber_scanf( &tmpber, "{eaa",/*}*/ &lderr,
559 &lr->lr_res_matched, &lr_res_error )
562 if ( lr_res_error != NULL ) {
563 if ( lr->lr_res_error != NULL ) {
564 (void)ldap_append_referral( ld, &lr->lr_res_error, lr_res_error );
565 LDAP_FREE( (char *)lr_res_error );
568 lr->lr_res_error = lr_res_error;
572 /* Check if V3 referral */
573 if ( ber_peek_tag( &tmpber, &len ) == LDAP_TAG_REFERRAL ) {
574 /* We have a V3 referral, assume we cannot chase it */
576 if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS)
577 || (lr->lr_parent != NULL) )
579 v3ref = -1; /* Assume referral not chased and return it to app */
580 /* Get the referral list */
581 if( ber_scanf( &tmpber, "{v}", &refs) == LBER_ERROR) {
582 rc = LDAP_DECODING_ERROR;
583 lr->lr_status = LDAP_REQST_COMPLETED;
584 Debug( LDAP_DEBUG_TRACE,
585 "read1msg: referral decode error, mark request completed, id = %d\n",
588 /* Chase the referral
589 * Note: refs arrary is freed by ldap_chase_v3referrals
591 refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
592 0, &lr->lr_res_error, &hadref );
593 lr->lr_status = LDAP_REQST_COMPLETED;
594 Debug( LDAP_DEBUG_TRACE,
595 "read1msg: referral chased, mark request completed, id = %d\n",
598 v3ref = 1; /* Referral successfully chased */
604 if( lr->lr_res_matched != NULL ) {
605 LDAP_FREE( lr->lr_res_matched );
606 lr->lr_res_matched = NULL;
608 if( lr->lr_res_error != NULL ) {
609 LDAP_FREE( lr->lr_res_error );
610 lr->lr_res_error = NULL;
616 /* All results that just return a status, i.e. don't return data
617 * go through the following code. This code also chases V2 referrals
618 * and checks if all referrals have been chased.
620 if ( (tag != LDAP_RES_SEARCH_ENTRY) && (v3ref > -1) &&
621 (tag != LDAP_RES_INTERMEDIATE ))
623 /* For a v3 search referral/reference, only come here if already chased it */
624 if ( ld->ld_version >= LDAP_VERSION2 &&
625 ( lr->lr_parent != NULL ||
626 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ) )
628 char *lr_res_error = NULL;
630 tmpber = *ber; /* struct copy */
632 /* V3 search reference or V3 referral
633 * sucessfully chased. If this message
634 * is a search result, then it has no more
635 * outstanding referrals.
637 if ( tag == LDAP_RES_SEARCH_RESULT )
639 } else if ( ber_scanf( &tmpber, "{eaa}", &lderr,
640 &lr->lr_res_matched, &lr_res_error )
643 if ( lr_res_error != NULL ) {
644 if ( lr->lr_res_error != NULL ) {
645 (void)ldap_append_referral( ld, &lr->lr_res_error, lr_res_error );
646 LDAP_FREE( (char *)lr_res_error );
648 lr->lr_res_error = lr_res_error;
652 if ( lderr != LDAP_SUCCESS ) {
653 /* referrals are in error string */
654 refer_cnt = ldap_chase_referrals( ld, lr,
655 &lr->lr_res_error, -1, &hadref );
656 lr->lr_status = LDAP_REQST_COMPLETED;
657 Debug( LDAP_DEBUG_TRACE,
658 "read1msg: V2 referral chased, mark request completed, id = %d\n", lr->lr_msgid, 0, 0);
661 /* save errno, message, and matched string */
662 if ( !hadref || lr->lr_res_error == NULL ) {
663 lr->lr_res_errno = ( lderr ==
664 LDAP_PARTIAL_RESULTS ) ? LDAP_SUCCESS
666 } else if ( ld->ld_errno != LDAP_SUCCESS ) {
667 lr->lr_res_errno = ld->ld_errno;
669 lr->lr_res_errno = LDAP_PARTIAL_RESULTS;
671 Debug( LDAP_DEBUG_TRACE,
672 "new result: res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
673 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
674 lr->lr_res_matched ? lr->lr_res_matched : "" );
678 Debug( LDAP_DEBUG_TRACE,
679 "read1msg: %d new referrals\n", refer_cnt, 0, 0 );
681 if ( refer_cnt != 0 ) { /* chasing referrals */
684 if ( refer_cnt < 0 ) {
685 return( -1 ); /* fatal error */
687 lr->lr_res_errno = LDAP_SUCCESS; /* sucessfully chased referral */
689 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
690 /* request without any referrals */
691 simple_request = ( hadref ? 0 : 1 );
693 /* request with referrals or child request */
698 lr->lr_status = LDAP_REQST_COMPLETED; /* declare this request done */
699 Debug( LDAP_DEBUG_TRACE,
700 "read1msg: mark request completed, id = %d\n", lr->lr_msgid, 0, 0);
701 while ( lr->lr_parent != NULL ) {
702 merge_error_info( ld, lr->lr_parent, lr );
705 if ( --lr->lr_outrefcnt > 0 ) {
706 break; /* not completely done yet */
710 /* Check if all requests are finished, lr is now parent */
712 if (tmplr->lr_status == LDAP_REQST_COMPLETED) {
713 for ( tmplr=lr->lr_child;
715 tmplr=tmplr->lr_refnext)
717 if( tmplr->lr_status != LDAP_REQST_COMPLETED) break;
721 /* This is the parent request if the request has referrals */
722 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL &&
726 tag = lr->lr_res_msgtype;
727 Debug( LDAP_DEBUG_ANY, "request %ld done\n",
729 Debug( LDAP_DEBUG_TRACE,
730 "res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
731 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
732 lr->lr_res_matched ? lr->lr_res_matched : "" );
733 if ( !simple_request ) {
736 if ( build_result_ber( ld, &ber, lr )
738 rc = -1; /* fatal error */
742 #ifdef LDAP_R_COMPILE
743 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
745 ldap_free_request( ld, lr );
746 #ifdef LDAP_R_COMPILE
747 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
752 #ifdef LDAP_R_COMPILE
753 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
755 ldap_free_connection( ld, lc, 0, 1 );
756 #ifdef LDAP_R_COMPILE
757 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
768 /* make a new ldap message */
769 newmsg = (LDAPMessage *) LDAP_CALLOC( 1, sizeof(LDAPMessage) );
770 if ( newmsg == NULL ) {
771 ld->ld_errno = LDAP_NO_MEMORY;
774 newmsg->lm_msgid = (int)id;
775 newmsg->lm_msgtype = tag;
776 newmsg->lm_ber = ber;
778 #ifdef LDAP_CONNECTIONLESS
779 /* CLDAP replies all fit in a single datagram. In LDAPv2 RFC1798
780 * the responses are all a sequence wrapped in one message. In
781 * LDAPv3 each response is in its own message. The datagram must
782 * end with a SearchResult. We can't just parse each response in
783 * separate calls to try_read1msg because the header info is only
784 * present at the beginning of the datagram, not at the beginning
785 * of each response. So parse all the responses at once and queue
786 * them up, then pull off the first response to return to the
787 * caller when all parsing is complete.
789 if ( LDAP_IS_UDP(ld) ) {
790 /* If not a result, look for more */
791 if ( tag != LDAP_RES_SEARCH_RESULT ) {
795 /* LDAPv2: dup the current ber, skip past the current
796 * response, and see if there are any more after it.
798 ber = ber_dup( ber );
799 ber_scanf( ber, "x" );
800 if (ber_peek_tag(ber, &len) != LBER_DEFAULT) {
801 /* There's more - dup the ber buffer so they can all be
802 * individually freed by ldap_msgfree.
805 ber_get_option(ber, LBER_OPT_BER_REMAINING_BYTES, &len);
806 bv.bv_val = LDAP_MALLOC(len);
809 ber_read(ber, bv.bv_val, len);
811 ber_init2(ber, &bv, ld->ld_lberoptions );
815 /* LDAPv3: Just allocate a new ber. Since this is a buffered
816 * datagram, if the sockbuf is readable we still have data
819 ber = ldap_alloc_ber_with_options(ld);
820 if (ber_sockbuf_ctrl(sb, LBER_SB_OPT_DATA_READY, NULL)) ok=1;
822 /* set up response chain */
825 newmsg->lm_next = ld->ld_responses;
826 ld->ld_responses = newmsg;
827 newmsg->lm_chain_tail = newmsg;
830 assert( tmp != NULL );
831 tmp->lm_chain = newmsg;
832 chain_head->lm_chain_tail = tmp;
835 /* "ok" means there's more to parse */
837 if (isv2) goto nextresp2;
840 /* got to end of datagram without a SearchResult. Free
841 * our dup'd ber, but leave any buffer alone. For v2 case,
842 * the previous response is still using this buffer. For v3,
843 * the new ber has no buffer to free yet.
848 } else if ( moremsgs ) {
849 /* got search result, and we had multiple responses in 1 datagram.
850 * stick the result onto the end of the chain, and then pull the
851 * first response off the head of the chain.
853 tmp->lm_chain = newmsg;
854 chain_head->lm_chain_tail = tmp;
855 *result = chkResponseList( ld, msgid, all );
856 ld->ld_errno = LDAP_SUCCESS;
857 return( (*result)->lm_msgtype );
860 #endif /* LDAP_CONNECTIONLESS */
862 /* is this the one we're looking for? */
863 if ( msgid == LDAP_RES_ANY || id == msgid ) {
864 if ( all == LDAP_MSG_ONE
865 || (newmsg->lm_msgtype != LDAP_RES_SEARCH_RESULT
866 && newmsg->lm_msgtype != LDAP_RES_SEARCH_ENTRY
867 && newmsg->lm_msgtype != LDAP_RES_SEARCH_REFERENCE) ) {
869 ld->ld_errno = LDAP_SUCCESS;
871 } else if ( newmsg->lm_msgtype == LDAP_RES_SEARCH_RESULT) {
872 foundit = 1; /* return the chain later */
877 * if not, we must add it to the list of responses. if
878 * the msgid is already there, it must be part of an existing
883 for ( l = ld->ld_responses; l != NULL; l = l->lm_next ) {
884 if ( l->lm_msgid == newmsg->lm_msgid )
889 /* not part of an existing search response */
896 newmsg->lm_next = ld->ld_responses;
897 ld->ld_responses = newmsg;
898 newmsg->lm_chain_tail = newmsg;
902 Debug( LDAP_DEBUG_TRACE, "adding response id %ld type %ld:\n",
903 (long) newmsg->lm_msgid, (long) newmsg->lm_msgtype, 0 );
905 /* part of a search response - add to end of list of entries */
906 if (l->lm_chain == NULL) {
907 assert(l->lm_chain_tail == l);
908 l->lm_chain = newmsg;
910 assert(l->lm_chain_tail != NULL);
911 assert(l->lm_chain_tail->lm_chain != NULL);
912 if ((l->lm_chain_tail->lm_chain->lm_msgtype
913 == LDAP_RES_SEARCH_ENTRY) ||
914 (l->lm_chain_tail->lm_chain->lm_msgtype
915 == LDAP_RES_SEARCH_REFERENCE) ||
916 (l->lm_chain_tail->lm_chain->lm_msgtype
917 == LDAP_RES_INTERMEDIATE)) {
918 l->lm_chain_tail->lm_chain->lm_chain = newmsg;
919 l->lm_chain_tail = l->lm_chain_tail->lm_chain;
921 /*FIXME: ldap_msgfree( l->lm_chain_tail->lm_chain );*/
922 l->lm_chain_tail->lm_chain = newmsg;
926 /* return the whole chain if that's what we were looking for */
929 ld->ld_responses = l->lm_next;
931 prev->lm_next = l->lm_next;
937 ld->ld_errno = LDAP_SUCCESS;
940 if ( ber_sockbuf_ctrl( sb, LBER_SB_OPT_DATA_READY, NULL ) ) {
943 return( -2 ); /* continue looking */
948 build_result_ber( LDAP *ld, BerElement **bp, LDAPRequest *lr )
956 ber = ldap_alloc_ber_with_options( ld );
959 ld->ld_errno = LDAP_NO_MEMORY;
963 if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
964 lr->lr_res_msgtype, lr->lr_res_errno,
965 lr->lr_res_matched ? lr->lr_res_matched : "",
966 lr->lr_res_error ? lr->lr_res_error : "" ) == -1 )
968 ld->ld_errno = LDAP_ENCODING_ERROR;
970 return( LBER_ERROR );
975 if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) {
976 ld->ld_errno = LDAP_DECODING_ERROR;
978 return( LBER_ERROR );
981 if ( ber_get_enum( ber, &along ) == LBER_ERROR ) {
982 ld->ld_errno = LDAP_DECODING_ERROR;
984 return( LBER_ERROR );
987 tag = ber_peek_tag( ber, &len );
989 if ( tag == LBER_ERROR ) {
990 ld->ld_errno = LDAP_DECODING_ERROR;
992 return( LBER_ERROR );
1001 merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
1004 * Merge error information in "lr" with "parentr" error code and string.
1006 if ( lr->lr_res_errno == LDAP_PARTIAL_RESULTS ) {
1007 parentr->lr_res_errno = lr->lr_res_errno;
1008 if ( lr->lr_res_error != NULL ) {
1009 (void)ldap_append_referral( ld, &parentr->lr_res_error,
1012 } else if ( lr->lr_res_errno != LDAP_SUCCESS &&
1013 parentr->lr_res_errno == LDAP_SUCCESS )
1015 parentr->lr_res_errno = lr->lr_res_errno;
1016 if ( parentr->lr_res_error != NULL ) {
1017 LDAP_FREE( parentr->lr_res_error );
1019 parentr->lr_res_error = lr->lr_res_error;
1020 lr->lr_res_error = NULL;
1021 if ( LDAP_NAME_ERROR( lr->lr_res_errno ) ) {
1022 if ( parentr->lr_res_matched != NULL ) {
1023 LDAP_FREE( parentr->lr_res_matched );
1025 parentr->lr_res_matched = lr->lr_res_matched;
1026 lr->lr_res_matched = NULL;
1030 Debug( LDAP_DEBUG_TRACE, "merged parent (id %d) error info: ",
1031 parentr->lr_msgid, 0, 0 );
1032 Debug( LDAP_DEBUG_TRACE, "result errno %d, error <%s>, matched <%s>\n",
1033 parentr->lr_res_errno, parentr->lr_res_error ?
1034 parentr->lr_res_error : "", parentr->lr_res_matched ?
1035 parentr->lr_res_matched : "" );
1041 ldap_msgtype( LDAPMessage *lm )
1043 assert( lm != NULL );
1044 return ( lm != NULL ) ? lm->lm_msgtype : -1;
1049 ldap_msgid( LDAPMessage *lm )
1051 assert( lm != NULL );
1053 return ( lm != NULL ) ? lm->lm_msgid : -1;
1057 char * ldap_int_msgtype2str( ber_tag_t tag )
1060 case LDAP_RES_ADD: return "add";
1061 case LDAP_RES_BIND: return "bind";
1062 case LDAP_RES_COMPARE: return "compare";
1063 case LDAP_RES_DELETE: return "delete";
1064 case LDAP_RES_EXTENDED: return "extended-result";
1065 case LDAP_RES_INTERMEDIATE: return "intermediate";
1066 case LDAP_RES_MODIFY: return "modify";
1067 case LDAP_RES_RENAME: return "rename";
1068 case LDAP_RES_SEARCH_ENTRY: return "search-entry";
1069 case LDAP_RES_SEARCH_REFERENCE: return "search-reference";
1070 case LDAP_RES_SEARCH_RESULT: return "search-result";
1076 ldap_msgfree( LDAPMessage *lm )
1081 Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
1083 for ( ; lm != NULL; lm = next ) {
1084 next = lm->lm_chain;
1085 type = lm->lm_msgtype;
1086 ber_free( lm->lm_ber, 1 );
1087 LDAP_FREE( (char *) lm );
1094 * ldap_msgdelete - delete a message. It returns:
1095 * 0 if the entire message was deleted
1096 * -1 if the message was not found, or only part of it was found
1099 ldap_msgdelete( LDAP *ld, int msgid )
1101 LDAPMessage *lm, *prev;
1104 assert( ld != NULL );
1106 Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete\n", 0, 0, 0 );
1109 #ifdef LDAP_R_COMPILE
1110 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
1112 for ( lm = ld->ld_responses; lm != NULL; lm = lm->lm_next ) {
1113 if ( lm->lm_msgid == msgid )
1122 ld->ld_responses = lm->lm_next;
1124 prev->lm_next = lm->lm_next;
1126 #ifdef LDAP_R_COMPILE
1127 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
1129 if ( lm && ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY )
1137 * return 1 if message msgid is waiting to be abandoned, 0 otherwise
1140 ldap_abandoned( LDAP *ld, ber_int_t msgid )
1144 if ( ld->ld_abandoned == NULL )
1147 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
1148 if ( ld->ld_abandoned[i] == msgid )
1156 ldap_mark_abandoned( LDAP *ld, ber_int_t msgid )
1160 if ( ld->ld_abandoned == NULL )
1163 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
1164 if ( ld->ld_abandoned[i] == msgid )
1167 if ( ld->ld_abandoned[i] == -1 )
1170 for ( ; ld->ld_abandoned[i] != -1; i++ ) {
1171 ld->ld_abandoned[i] = ld->ld_abandoned[i + 1];