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 = lc->lconn_next ) {
319 if ( ber_sockbuf_ctrl( lc->lconn_sb,
320 LBER_SB_OPT_DATA_READY, NULL ) ) {
321 rc = try_read1msg( ld, msgid, all, lc->lconn_sb,
328 rc = ldap_int_select( ld, tvp );
332 LDAP_LOG ( OPERATION, ARGS,
333 "wait4msg: ldap_int_select returned -1: errno %d\n",
336 Debug( LDAP_DEBUG_TRACE,
337 "ldap_int_select returned -1: errno %d\n",
343 if ( rc == 0 || ( rc == -1 && (
344 !LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART)
345 || errno != EINTR )))
347 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
353 rc = -2; /* select interrupted: loop */
356 #ifdef LDAP_R_COMPILE
357 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
359 if ( ld->ld_requests &&
360 ld->ld_requests->lr_status == LDAP_REQST_WRITING &&
361 ldap_is_write_ready( ld,
362 ld->ld_requests->lr_conn->lconn_sb ) ) {
363 ldap_int_flush_request( ld, ld->ld_requests );
365 #ifdef LDAP_R_COMPILE
366 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
368 for ( lc = ld->ld_conns; rc == -2 && lc != NULL;
370 nextlc = lc->lconn_next;
371 if ( lc->lconn_status ==
372 LDAP_CONNST_CONNECTED &&
373 ldap_is_read_ready( ld,
375 rc = try_read1msg( ld, msgid, all,
376 lc->lconn_sb, lc, result );
383 if ( rc == -2 && tvp != NULL ) {
384 tmp_time = time( NULL );
385 if (( tv.tv_sec -= ( tmp_time - start_time )) <= 0 ) {
386 rc = 0; /* timed out */
387 ld->ld_errno = LDAP_TIMEOUT;
392 LDAP_LOG ( OPERATION, DETAIL1,
393 "wait4msg: %ld secs to go\n", (long) tv.tv_sec, 0, 0 );
395 Debug( LDAP_DEBUG_TRACE, "wait4msg: %ld secs to go\n",
396 (long) tv.tv_sec, 0, 0 );
398 start_time = tmp_time;
413 LDAPMessage **result )
416 LDAPMessage *new, *l, *prev, *tmp;
421 LDAPRequest *lr, *tmplr;
423 int rc, refer_cnt, hadref, simple_request;
425 #ifdef LDAP_CONNECTIONLESS
426 int firstmsg = 1, moremsgs = 0, isv2 = 0;
429 * v3ref = flag for V3 referral / search reference
430 * 0 = not a ref, 1 = sucessfully chased ref, -1 = pass ref to application
434 assert( ld != NULL );
435 assert( lc != NULL );
438 LDAP_LOG ( OPERATION, ARGS, "read1msg: msgid %d, all %d\n", msgid, all, 0 );
440 Debug( LDAP_DEBUG_TRACE, "read1msg: msgid %d, all %d\n", msgid, all, 0 );
444 if ( lc->lconn_ber == NULL ) {
445 lc->lconn_ber = ldap_alloc_ber_with_options(ld);
447 if( lc->lconn_ber == NULL ) {
453 assert( LBER_VALID (ber) );
455 /* get the next message */
457 #ifdef LDAP_CONNECTIONLESS
458 if ( LDAP_IS_UDP(ld) ) {
459 struct sockaddr from;
460 ber_int_sb_read(sb, &from, sizeof(struct sockaddr));
461 if (ld->ld_options.ldo_version == LDAP_VERSION2) isv2=1;
465 tag = ber_get_next( sb, &len, ber );
466 if ( tag == LDAP_TAG_MESSAGE ) {
468 * We read a complete message.
469 * The connection should no longer need this ber.
471 lc->lconn_ber = NULL;
473 if ( tag != LDAP_TAG_MESSAGE ) {
474 if ( tag == LBER_DEFAULT) {
477 LDAP_LOG ( OPERATION, DETAIL1,
478 "read1msg: ber_get_next failed\n", 0, 0, 0 );
480 Debug( LDAP_DEBUG_CONNS,
481 "ber_get_next failed.\n", 0, 0, 0 );
485 if (errno==EWOULDBLOCK) return -2;
488 if (errno == EAGAIN) return -2;
490 ld->ld_errno = LDAP_SERVER_DOWN;
493 ld->ld_errno = LDAP_LOCAL_ERROR;
498 if ( ber_get_int( ber, &id ) == LBER_ERROR ) {
500 ld->ld_errno = LDAP_DECODING_ERROR;
504 /* if it's been abandoned, toss it */
505 if ( ldap_abandoned( ld, id ) ) {
507 LDAP_LOG ( OPERATION, DETAIL1, "read1msg: abandoned\n", 0, 0, 0 );
509 Debug( LDAP_DEBUG_ANY, "abandoned\n", 0, 0, 0);
513 if ( ber_sockbuf_ctrl( sb, LBER_SB_OPT_DATA_READY, NULL ) ) {
516 return( -2 ); /* continue looking */
519 if (( lr = ldap_find_request_by_msgid( ld, id )) == NULL ) {
521 LDAP_LOG ( OPERATION, DETAIL1,
522 "read1msg: no request for response with msgid %ld (tossing)\n",
525 Debug( LDAP_DEBUG_ANY,
526 "no request for response with msgid %ld (tossing)\n",
531 #ifdef LDAP_CONNECTIONLESS
532 if (LDAP_IS_UDP(ld) && isv2) {
533 ber_scanf(ber, "x{");
537 /* the message type */
538 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
539 ld->ld_errno = LDAP_DECODING_ERROR;
545 LDAP_LOG ( OPERATION, DETAIL1,
546 "read1msg: ldap_read: message type %s msgid %ld, original id %ld\n",
547 ldap_int_msgtype2str( tag ),
548 (long) lr->lr_msgid, (long) lr->lr_origid );
550 Debug( LDAP_DEBUG_TRACE,
551 "ldap_read: message type %s msgid %ld, original id %ld\n",
552 ldap_int_msgtype2str( tag ),
553 (long) lr->lr_msgid, (long) lr->lr_origid );
558 hadref = simple_request = 0;
559 rc = -2; /* default is to keep looking (no response found) */
560 lr->lr_res_msgtype = tag;
563 * This code figures out if we are going to chase a
564 * referral / search reference, or pass it back to the application
566 v3ref = 0; /* Assume not a V3 search reference or referral */
567 if( (tag != LDAP_RES_SEARCH_ENTRY) && (ld->ld_version > LDAP_VERSION2) ) {
568 BerElement tmpber = *ber; /* struct copy */
571 if( tag == LDAP_RES_SEARCH_REFERENCE) {
572 /* This is a V3 search reference */
573 /* Assume we do not chase the reference, but pass it to application */
575 if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ||
576 (lr->lr_parent != NULL) )
578 /* Get the referral list */
579 if ( ber_scanf( &tmpber, "{v}", &refs ) == LBER_ERROR ) {
580 rc = LDAP_DECODING_ERROR;
582 /* Note: refs arrary is freed by ldap_chase_v3referrals */
583 refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
584 1, &lr->lr_res_error, &hadref );
585 if ( refer_cnt > 0 ) { /* sucessfully chased reference */
586 /* If haven't got end search, set chasing referrals */
587 if( lr->lr_status != LDAP_REQST_COMPLETED) {
588 lr->lr_status = LDAP_REQST_CHASINGREFS;
590 LDAP_LOG ( OPERATION, DETAIL1,
591 "read1msg: search ref chased,"
592 "mark request chasing refs, id = %d\n",
593 lr->lr_msgid, 0, 0 );
595 Debug( LDAP_DEBUG_TRACE,
596 "read1msg: search ref chased, mark request chasing refs, id = %d\n",
600 v3ref = 1; /* We sucessfully chased the reference */
605 /* Check for V3 referral */
607 if ( ber_scanf( &tmpber, "{iaa",/*}*/ &lderr,
608 &lr->lr_res_matched, &lr->lr_res_error )
610 /* Check if V3 referral */
611 if( ber_peek_tag( &tmpber, &len) == LDAP_TAG_REFERRAL ) {
612 /* We have a V3 referral, assume we cannot chase it */
614 if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS)
615 || (lr->lr_parent != NULL) )
617 v3ref = -1; /* Assume referral not chased and return it to app */
618 /* Get the referral list */
619 if( ber_scanf( &tmpber, "{v}", &refs) == LBER_ERROR) {
620 rc = LDAP_DECODING_ERROR;
621 lr->lr_status = LDAP_REQST_COMPLETED;
623 LDAP_LOG ( OPERATION, DETAIL1,
624 "read1msg: referral decode error,"
625 "mark request completed, id = %d\n",
626 lr->lr_msgid, 0, 0 );
628 Debug( LDAP_DEBUG_TRACE,
629 "read1msg: referral decode error, mark request completed, id = %d\n",
633 /* Chase the referral
634 * Note: refs arrary is freed by ldap_chase_v3referrals
636 refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
637 0, &lr->lr_res_error, &hadref );
638 lr->lr_status = LDAP_REQST_COMPLETED;
640 LDAP_LOG ( OPERATION, DETAIL1,
641 "read1msg: referral chased,"
642 "mark request completed, id = %d\n",
643 lr->lr_msgid, 0, 0 );
645 Debug( LDAP_DEBUG_TRACE,
646 "read1msg: referral chased, mark request completed, id = %d\n",
650 v3ref = 1; /* Referral successfully chased */
656 if( lr->lr_res_matched != NULL ) {
657 LDAP_FREE( lr->lr_res_matched );
658 lr->lr_res_matched = NULL;
660 if( lr->lr_res_error != NULL ) {
661 LDAP_FREE( lr->lr_res_error );
662 lr->lr_res_error = NULL;
668 /* All results that just return a status, i.e. don't return data
669 * go through the following code. This code also chases V2 referrals
670 * and checks if all referrals have been chased.
672 if ( (tag != LDAP_RES_SEARCH_ENTRY) && (v3ref > -1) &&
673 (tag != LDAP_RES_INTERMEDIATE ))
675 /* For a v3 search referral/reference, only come here if already chased it */
676 if ( ld->ld_version >= LDAP_VERSION2 &&
677 ( lr->lr_parent != NULL ||
678 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ) )
680 tmpber = *ber; /* struct copy */
682 /* V3 search reference or V3 referral
683 * sucessfully chased. If this message
684 * is a search result, then it has no more
685 * outstanding referrals.
687 if ( tag == LDAP_RES_SEARCH_RESULT )
689 } else if ( ber_scanf( &tmpber, "{iaa}", &lderr,
690 &lr->lr_res_matched, &lr->lr_res_error )
692 if ( lderr != LDAP_SUCCESS ) {
693 /* referrals are in error string */
694 refer_cnt = ldap_chase_referrals( ld, lr,
695 &lr->lr_res_error, -1, &hadref );
696 lr->lr_status = LDAP_REQST_COMPLETED;
698 LDAP_LOG ( OPERATION, DETAIL1,
699 "read1msg: V2 referral chased,"
700 "mark request completed, id = %d\n",
701 lr->lr_msgid, 0, 0 );
703 Debug( LDAP_DEBUG_TRACE,
704 "read1msg: V2 referral chased, mark request completed, id = %d\n", lr->lr_msgid, 0, 0);
708 /* save errno, message, and matched string */
709 if ( !hadref || lr->lr_res_error == NULL ) {
710 lr->lr_res_errno = ( lderr ==
711 LDAP_PARTIAL_RESULTS ) ? LDAP_SUCCESS
713 } else if ( ld->ld_errno != LDAP_SUCCESS ) {
714 lr->lr_res_errno = ld->ld_errno;
716 lr->lr_res_errno = LDAP_PARTIAL_RESULTS;
719 LDAP_LOG ( OPERATION, DETAIL1,
720 "read1msg: new result: res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
721 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
722 lr->lr_res_matched ? lr->lr_res_matched : "" );
724 Debug( LDAP_DEBUG_TRACE,
725 "new result: res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
726 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
727 lr->lr_res_matched ? lr->lr_res_matched : "" );
733 LDAP_LOG ( OPERATION, DETAIL1, "read1msg: %d new referrals\n",
736 Debug( LDAP_DEBUG_TRACE,
737 "read1msg: %d new referrals\n", refer_cnt, 0, 0 );
740 if ( refer_cnt != 0 ) { /* chasing referrals */
743 if ( refer_cnt < 0 ) {
744 return( -1 ); /* fatal error */
746 lr->lr_res_errno = LDAP_SUCCESS; /* sucessfully chased referral */
748 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
749 /* request without any referrals */
750 simple_request = ( hadref ? 0 : 1 );
752 /* request with referrals or child request */
757 lr->lr_status = LDAP_REQST_COMPLETED; /* declare this request done */
759 LDAP_LOG ( OPERATION, DETAIL1,
760 "read1msg: mark request completed, id = %d\n",
761 lr->lr_msgid, 0, 0 );
763 Debug( LDAP_DEBUG_TRACE,
764 "read1msg: mark request completed, id = %d\n", lr->lr_msgid, 0, 0);
766 while ( lr->lr_parent != NULL ) {
767 merge_error_info( ld, lr->lr_parent, lr );
770 if ( --lr->lr_outrefcnt > 0 ) {
771 break; /* not completely done yet */
775 /* Check if all requests are finished, lr is now parent */
777 if (tmplr->lr_status == LDAP_REQST_COMPLETED) {
778 for(tmplr=lr->lr_child; tmplr != NULL; tmplr=tmplr->lr_refnext) {
779 if( tmplr->lr_status != LDAP_REQST_COMPLETED) {
785 /* This is the parent request if the request has referrals */
786 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL && tmplr == NULL ) {
788 tag = lr->lr_res_msgtype;
790 LDAP_LOG ( OPERATION, DETAIL1,
791 "read1msg: request %ld done\n", (long) id, 0, 0 );
792 LDAP_LOG ( OPERATION, DETAIL1,
793 "read1msg: res_errno: %d,res_error: <%s>, res_matched: <%s>\n",
794 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
795 lr->lr_res_matched ? lr->lr_res_matched : "" );
797 Debug( LDAP_DEBUG_ANY, "request %ld done\n",
799 Debug( LDAP_DEBUG_TRACE,
800 "res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
801 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
802 lr->lr_res_matched ? lr->lr_res_matched : "" );
804 if ( !simple_request ) {
807 if ( build_result_ber( ld, &ber, lr )
809 rc = -1; /* fatal error */
813 ldap_free_request( ld, lr );
817 ldap_free_connection( ld, lc, 0, 1 );
826 /* make a new ldap message */
827 if ( (new = (LDAPMessage *) LDAP_CALLOC( 1, sizeof(LDAPMessage) ))
829 ld->ld_errno = LDAP_NO_MEMORY;
832 new->lm_msgid = (int)id;
833 new->lm_msgtype = tag;
836 #ifdef LDAP_CONNECTIONLESS
837 /* CLDAP replies all fit in a single datagram. In LDAPv2 RFC1798
838 * the responses are all a sequence wrapped in one message. In
839 * LDAPv3 each response is in its own message. The datagram must
840 * end with a SearchResult. We can't just parse each response in
841 * separate calls to try_read1msg because the header info is only
842 * present at the beginning of the datagram, not at the beginning
843 * of each response. So parse all the responses at once and queue
844 * them up, then pull off the first response to return to the
845 * caller when all parsing is complete.
847 if ( LDAP_IS_UDP(ld) ) {
848 /* If not a result, look for more */
849 if ( tag != LDAP_RES_SEARCH_RESULT ) {
853 /* LDAPv2: dup the current ber, skip past the current
854 * response, and see if there are any more after it.
856 ber = ber_dup( ber );
857 ber_scanf( ber, "x" );
858 if (ber_peek_tag(ber, &len) != LBER_DEFAULT) {
859 /* There's more - dup the ber buffer so they can all be
860 * individually freed by ldap_msgfree.
863 ber_get_option(ber, LBER_OPT_BER_REMAINING_BYTES, &len);
864 bv.bv_val = LDAP_MALLOC(len);
867 ber_read(ber, bv.bv_val, len);
869 ber_init2(ber, &bv, ld->ld_lberoptions );
873 /* LDAPv3: Just allocate a new ber. Since this is a buffered
874 * datagram, if the sockbuf is readable we still have data
877 ber = ldap_alloc_ber_with_options(ld);
878 if (ber_sockbuf_ctrl(sb, LBER_SB_OPT_DATA_READY, NULL)) ok=1;
880 /* set up response chain */
883 new->lm_next = ld->ld_responses;
884 ld->ld_responses = new;
889 /* "ok" means there's more to parse */
891 if (isv2) goto nextresp2;
894 /* got to end of datagram without a SearchResult. Free
895 * our dup'd ber, but leave any buffer alone. For v2 case,
896 * the previous response is still using this buffer. For v3,
897 * the new ber has no buffer to free yet.
902 } else if ( moremsgs ) {
903 /* got search result, and we had multiple responses in 1 datagram.
904 * stick the result onto the end of the chain, and then pull the
905 * first response off the head of the chain.
908 *result = chkResponseList( ld, msgid, all );
909 ld->ld_errno = LDAP_SUCCESS;
910 return( (*result)->lm_msgtype );
915 /* is this the one we're looking for? */
916 if ( msgid == LDAP_RES_ANY || id == msgid ) {
917 if ( all == LDAP_MSG_ONE
918 || (new->lm_msgtype != LDAP_RES_SEARCH_RESULT
919 && new->lm_msgtype != LDAP_RES_SEARCH_ENTRY
920 && new->lm_msgtype != LDAP_RES_SEARCH_REFERENCE) ) {
922 ld->ld_errno = LDAP_SUCCESS;
924 } else if ( new->lm_msgtype == LDAP_RES_SEARCH_RESULT) {
925 foundit = 1; /* return the chain later */
930 * if not, we must add it to the list of responses. if
931 * the msgid is already there, it must be part of an existing
936 for ( l = ld->ld_responses; l != NULL; l = l->lm_next ) {
937 if ( l->lm_msgid == new->lm_msgid )
942 /* not part of an existing search response */
949 new->lm_next = ld->ld_responses;
950 ld->ld_responses = new;
955 LDAP_LOG ( OPERATION, DETAIL1,
956 "read1msg: adding response id %ld type %ld\n",
957 (long) new->lm_msgid, (long) new->lm_msgtype, 0 );
959 Debug( LDAP_DEBUG_TRACE, "adding response id %ld type %ld:\n",
960 (long) new->lm_msgid, (long) new->lm_msgtype, 0 );
963 /* part of a search response - add to end of list of entries */
964 for ( tmp = l; (tmp->lm_chain != NULL) &&
965 ((tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY) ||
966 (tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_REFERENCE) ||
967 (tmp->lm_chain->lm_msgtype == LDAP_RES_INTERMEDIATE ));
968 tmp = tmp->lm_chain )
972 /* return the whole chain if that's what we were looking for */
975 ld->ld_responses = l->lm_next;
977 prev->lm_next = l->lm_next;
983 ld->ld_errno = LDAP_SUCCESS;
986 if ( ber_sockbuf_ctrl( sb, LBER_SB_OPT_DATA_READY, NULL ) ) {
989 return( -2 ); /* continue looking */
994 build_result_ber( LDAP *ld, BerElement **bp, LDAPRequest *lr )
1002 ber = ldap_alloc_ber_with_options( ld );
1005 ld->ld_errno = LDAP_NO_MEMORY;
1009 if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
1010 lr->lr_res_msgtype, lr->lr_res_errno,
1011 lr->lr_res_matched ? lr->lr_res_matched : "",
1012 lr->lr_res_error ? lr->lr_res_error : "" ) == -1 ) {
1014 ld->ld_errno = LDAP_ENCODING_ERROR;
1016 return( LBER_ERROR );
1019 ber_reset( ber, 1 );
1021 if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) {
1022 ld->ld_errno = LDAP_DECODING_ERROR;
1024 return( LBER_ERROR );
1027 if ( ber_get_int( ber, &along ) == LBER_ERROR ) {
1028 ld->ld_errno = LDAP_DECODING_ERROR;
1030 return( LBER_ERROR );
1033 tag = ber_peek_tag( ber, &len );
1035 if ( tag == LBER_ERROR ) {
1036 ld->ld_errno = LDAP_DECODING_ERROR;
1038 return( LBER_ERROR );
1047 merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
1050 * Merge error information in "lr" with "parentr" error code and string.
1052 if ( lr->lr_res_errno == LDAP_PARTIAL_RESULTS ) {
1053 parentr->lr_res_errno = lr->lr_res_errno;
1054 if ( lr->lr_res_error != NULL ) {
1055 (void)ldap_append_referral( ld, &parentr->lr_res_error,
1058 } else if ( lr->lr_res_errno != LDAP_SUCCESS &&
1059 parentr->lr_res_errno == LDAP_SUCCESS ) {
1060 parentr->lr_res_errno = lr->lr_res_errno;
1061 if ( parentr->lr_res_error != NULL ) {
1062 LDAP_FREE( parentr->lr_res_error );
1064 parentr->lr_res_error = lr->lr_res_error;
1065 lr->lr_res_error = NULL;
1066 if ( LDAP_NAME_ERROR( lr->lr_res_errno )) {
1067 if ( parentr->lr_res_matched != NULL ) {
1068 LDAP_FREE( parentr->lr_res_matched );
1070 parentr->lr_res_matched = lr->lr_res_matched;
1071 lr->lr_res_matched = NULL;
1076 LDAP_LOG( OPERATION, DETAIL1, "merged parent (id %d) error info: ",
1077 parentr->lr_msgid, 0, 0 );
1078 LDAP_LOG( OPERATION, DETAIL1, "result errno %d, error <%s>, matched <%s>\n",
1079 parentr->lr_res_errno, parentr->lr_res_error ?
1080 parentr->lr_res_error : "", parentr->lr_res_matched ?
1081 parentr->lr_res_matched : "" );
1083 Debug( LDAP_DEBUG_TRACE, "merged parent (id %d) error info: ",
1084 parentr->lr_msgid, 0, 0 );
1085 Debug( LDAP_DEBUG_TRACE, "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 : "" );
1095 ldap_msgtype( LDAPMessage *lm )
1097 assert( lm != NULL );
1098 return ( lm != NULL ) ? lm->lm_msgtype : -1;
1103 ldap_msgid( LDAPMessage *lm )
1105 assert( lm != NULL );
1107 return ( lm != NULL ) ? lm->lm_msgid : -1;
1111 char * ldap_int_msgtype2str( ber_tag_t tag )
1114 case LDAP_RES_ADD: return "add";
1115 case LDAP_RES_BIND: return "bind";
1116 case LDAP_RES_COMPARE: return "compare";
1117 case LDAP_RES_DELETE: return "delete";
1118 case LDAP_RES_EXTENDED: return "extended-result";
1119 case LDAP_RES_INTERMEDIATE: return "intermediate";
1120 case LDAP_RES_MODIFY: return "modify";
1121 case LDAP_RES_RENAME: return "rename";
1122 case LDAP_RES_SEARCH_ENTRY: return "search-entry";
1123 case LDAP_RES_SEARCH_REFERENCE: return "search-reference";
1124 case LDAP_RES_SEARCH_RESULT: return "search-result";
1130 ldap_msgfree( LDAPMessage *lm )
1136 LDAP_LOG ( OPERATION, ENTRY, "ldap_msgfree\n", 0, 0, 0 );
1138 Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
1141 for ( ; lm != NULL; lm = next ) {
1142 next = lm->lm_chain;
1143 type = lm->lm_msgtype;
1144 ber_free( lm->lm_ber, 1 );
1145 LDAP_FREE( (char *) lm );
1152 * ldap_msgdelete - delete a message. It returns:
1153 * 0 if the entire message was deleted
1154 * -1 if the message was not found, or only part of it was found
1157 ldap_msgdelete( LDAP *ld, int msgid )
1159 LDAPMessage *lm, *prev;
1162 assert( ld != NULL );
1165 LDAP_LOG ( OPERATION, ENTRY, "ldap_msgdelete\n", 0, 0, 0 );
1167 Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete\n", 0, 0, 0 );
1171 #ifdef LDAP_R_COMPILE
1172 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
1174 for ( lm = ld->ld_responses; lm != NULL; lm = lm->lm_next ) {
1175 if ( lm->lm_msgid == msgid )
1184 ld->ld_responses = lm->lm_next;
1186 prev->lm_next = lm->lm_next;
1188 #ifdef LDAP_R_COMPILE
1189 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
1191 if ( lm && ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY )
1199 * return 1 if message msgid is waiting to be abandoned, 0 otherwise
1202 ldap_abandoned( LDAP *ld, ber_int_t msgid )
1206 if ( ld->ld_abandoned == NULL )
1209 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
1210 if ( ld->ld_abandoned[i] == msgid )
1218 ldap_mark_abandoned( LDAP *ld, ber_int_t msgid )
1222 if ( ld->ld_abandoned == NULL )
1225 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
1226 if ( ld->ld_abandoned[i] == msgid )
1229 if ( ld->ld_abandoned[i] == -1 )
1232 for ( ; ld->ld_abandoned[i] != -1; i++ ) {
1233 ld->ld_abandoned[i] = ld->ld_abandoned[i + 1];