1 /* result.c - wait for an ldap result */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2006 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.
43 * LDAPResult ::= SEQUENCE {
44 * resultCode ENUMERATED { ... },
46 * diagnosticMessage LDAPString,
47 * referral [3] Referral OPTIONAL
49 * Referral ::= SEQUENCE OF LDAPURL (one or more)
50 * LDAPURL ::= LDAPString (limited to URL chars)
57 #include <ac/stdlib.h>
60 #include <ac/socket.h>
61 #include <ac/string.h>
63 #include <ac/unistd.h>
69 static int ldap_abandoned LDAP_P(( LDAP *ld, ber_int_t msgid, int *idx ));
70 static int ldap_mark_abandoned LDAP_P(( LDAP *ld, ber_int_t msgid, int idx ));
71 static int wait4msg LDAP_P(( LDAP *ld, ber_int_t msgid, int all, struct timeval *timeout,
72 LDAPMessage **result ));
73 static ber_tag_t try_read1msg LDAP_P(( LDAP *ld, ber_int_t msgid,
74 int all, LDAPConn **lc, LDAPMessage **result ));
75 static ber_tag_t build_result_ber LDAP_P(( LDAP *ld, BerElement **bp, LDAPRequest *lr ));
76 static void merge_error_info LDAP_P(( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr ));
77 static LDAPMessage * chkResponseList LDAP_P(( LDAP *ld, int msgid, int all));
79 #define LDAP_MSG_X_KEEP_LOOKING (-2)
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 )
108 LDAPMessage *lm = NULL;
111 assert( ld != NULL );
112 assert( result != NULL );
114 Debug( LDAP_DEBUG_TRACE, "ldap_result ld %p msgid %d\n", (void *)ld, msgid, 0 );
116 #ifdef LDAP_R_COMPILE
117 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
121 /* this is already done inside wait4msg(), right?... */
122 lm = chkResponseList( ld, msgid, all );
126 rc = wait4msg( ld, msgid, all, timeout, result );
130 ld->ld_errno = LDAP_SUCCESS;
134 #ifdef LDAP_R_COMPILE
135 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
147 LDAPMessage *lm, **lastlm, *nextlm;
151 * Look through the list of responses we have received on
152 * this association and see if the response we're interested in
153 * is there. If it is, return it. If not, call wait4msg() to
154 * wait until it arrives or timeout occurs.
157 #ifdef LDAP_R_COMPILE
158 LDAP_PVT_THREAD_ASSERT_MUTEX_OWNER( &ld->ld_res_mutex );
161 Debug( LDAP_DEBUG_TRACE,
162 "ldap_chkResponseList ld %p msgid %d all %d\n",
163 (void *)ld, msgid, all );
165 lastlm = &ld->ld_responses;
166 for ( lm = ld->ld_responses; lm != NULL; lm = nextlm ) {
169 nextlm = lm->lm_next;
172 if ( ldap_abandoned( ld, lm->lm_msgid, &idx ) ) {
173 Debug( LDAP_DEBUG_ANY,
174 "response list msg abandoned, "
175 "msgid %d message type %s\n",
176 lm->lm_msgid, ldap_int_msgtype2str( lm->lm_msgtype ), 0 );
178 switch ( lm->lm_msgtype ) {
179 case LDAP_RES_SEARCH_ENTRY:
180 case LDAP_RES_SEARCH_REFERENCE:
181 case LDAP_RES_INTERMEDIATE:
185 /* there's no need to keep the id
186 * in the abandoned list any longer */
187 ldap_mark_abandoned( ld, lm->lm_msgid, idx );
191 /* Remove this entry from list */
199 if ( msgid == LDAP_RES_ANY || lm->lm_msgid == msgid ) {
202 if ( all == LDAP_MSG_ONE ||
203 all == LDAP_MSG_RECEIVED ||
204 msgid == LDAP_RES_UNSOLICITED )
209 tmp = lm->lm_chain_tail;
210 if ( tmp->lm_msgtype == LDAP_RES_SEARCH_ENTRY ||
211 tmp->lm_msgtype == LDAP_RES_SEARCH_REFERENCE ||
212 tmp->lm_msgtype == LDAP_RES_INTERMEDIATE )
223 lastlm = &lm->lm_next;
227 /* Found an entry, remove it from the list */
228 if ( all == LDAP_MSG_ONE && lm->lm_chain != NULL ) {
229 *lastlm = lm->lm_chain;
230 lm->lm_chain->lm_next = lm->lm_next;
231 lm->lm_chain->lm_chain_tail = ( lm->lm_chain_tail != lm ) ? lm->lm_chain_tail : lm->lm_chain;
233 lm->lm_chain_tail = NULL;
235 *lastlm = lm->lm_next;
242 Debug( LDAP_DEBUG_TRACE,
243 "ldap_chkResponseList returns ld %p NULL\n", (void *)ld, 0, 0);
245 Debug( LDAP_DEBUG_TRACE,
246 "ldap_chkResponseList returns ld %p msgid %d, type 0x%02lu\n",
247 (void *)ld, lm->lm_msgid, (unsigned long)lm->lm_msgtype );
259 struct timeval *timeout,
260 LDAPMessage **result )
263 struct timeval tv = { 0 },
266 time_t start_time = 0;
270 assert( ld != NULL );
271 assert( result != NULL );
273 #ifdef LDAP_R_COMPILE
274 LDAP_PVT_THREAD_ASSERT_MUTEX_OWNER( &ld->ld_res_mutex );
278 if ( timeout == NULL ) {
279 Debug( LDAP_DEBUG_TRACE, "wait4msg ld %p msgid %d (infinite timeout)\n",
280 (void *)ld, msgid, 0 );
282 Debug( LDAP_DEBUG_TRACE, "wait4msg ld %p msgid %d (timeout %ld usec)\n",
283 (void *)ld, msgid, (long)timeout->tv_sec * 1000000 + timeout->tv_usec );
285 #endif /* LDAP_DEBUG */
287 if ( timeout == NULL ) {
293 start_time = time( NULL );
296 rc = LDAP_MSG_X_KEEP_LOOKING;
297 while ( rc == LDAP_MSG_X_KEEP_LOOKING ) {
299 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
300 Debug( LDAP_DEBUG_TRACE, "wait4msg continue ld %p msgid %d all %d\n",
301 (void *)ld, msgid, all );
302 #ifdef LDAP_R_COMPILE
303 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
305 ldap_dump_connection( ld, ld->ld_conns, 1 );
306 #ifdef LDAP_R_COMPILE
307 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
308 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
310 ldap_dump_requests_and_responses( ld );
311 #ifdef LDAP_R_COMPILE
312 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
315 #endif /* LDAP_DEBUG */
317 if ( ( *result = chkResponseList( ld, msgid, all ) ) != NULL ) {
318 rc = (*result)->lm_msgtype;
323 #ifdef LDAP_R_COMPILE
324 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
326 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
327 if ( ber_sockbuf_ctrl( lc->lconn_sb,
328 LBER_SB_OPT_DATA_READY, NULL ) )
330 #ifdef LDAP_R_COMPILE
331 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
333 rc = try_read1msg( ld, msgid, all, &lc, result );
334 #ifdef LDAP_R_COMPILE
335 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
341 #ifdef LDAP_R_COMPILE
342 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
346 rc = ldap_int_select( ld, tvp );
349 Debug( LDAP_DEBUG_TRACE,
350 "ldap_int_select returned -1: errno %d\n",
351 sock_errno(), 0, 0 );
355 if ( rc == 0 || ( rc == -1 && (
356 !LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART)
357 || sock_errno() != EINTR ) ) )
359 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
365 rc = LDAP_MSG_X_KEEP_LOOKING; /* select interrupted: loop */
368 rc = LDAP_MSG_X_KEEP_LOOKING;
369 #ifdef LDAP_R_COMPILE
370 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
372 if ( ld->ld_requests &&
373 ld->ld_requests->lr_status == LDAP_REQST_WRITING &&
374 ldap_is_write_ready( ld,
375 ld->ld_requests->lr_conn->lconn_sb ) )
377 ldap_int_flush_request( ld, ld->ld_requests );
379 #ifdef LDAP_R_COMPILE
380 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
381 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
383 for ( lc = ld->ld_conns;
384 rc == LDAP_MSG_X_KEEP_LOOKING && lc != NULL; )
386 if ( lc->lconn_status == LDAP_CONNST_CONNECTED &&
387 ldap_is_read_ready( ld, lc->lconn_sb ) )
389 #ifdef LDAP_R_COMPILE
390 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
392 rc = try_read1msg( ld, msgid, all, &lc, result );
393 #ifdef LDAP_R_COMPILE
394 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
397 /* if lc gets free()'d,
398 * there's no guarantee
399 * lc->lconn_next is still
400 * sane; better restart
404 /* don't get to next conn! */
412 #ifdef LDAP_R_COMPILE
413 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
419 if ( rc == LDAP_MSG_X_KEEP_LOOKING && tvp != NULL ) {
420 tmp_time = time( NULL );
421 tv0.tv_sec -= ( tmp_time - start_time );
422 if ( tv0.tv_sec <= 0 ) {
423 rc = 0; /* timed out */
424 ld->ld_errno = LDAP_TIMEOUT;
427 tv.tv_sec = tv0.tv_sec;
429 Debug( LDAP_DEBUG_TRACE, "wait4msg ld %p %ld secs to go\n",
430 (void *)ld, (long) tv.tv_sec, 0 );
431 start_time = tmp_time;
445 LDAPMessage **result )
448 LDAPMessage *newmsg, *l, *prev;
454 LDAPRequest *lr, *tmplr;
457 int rc, refer_cnt, hadref, simple_request;
460 #ifdef LDAP_CONNECTIONLESS
461 LDAPMessage *tmp = NULL, *chain_head = NULL;
462 int moremsgs = 0, isv2 = 0;
466 * v3ref = flag for V3 referral / search reference
467 * 0 = not a ref, 1 = sucessfully chased ref, -1 = pass ref to application
475 assert( ld != NULL );
476 assert( lcp != NULL );
477 assert( *lcp != NULL );
479 #ifdef LDAP_R_COMPILE
480 LDAP_PVT_THREAD_ASSERT_MUTEX_OWNER( &ld->ld_res_mutex );
483 Debug( LDAP_DEBUG_TRACE, "read1msg: ld %p msgid %d all %d\n",
484 (void *)ld, msgid, all );
489 if ( lc->lconn_ber == NULL ) {
490 lc->lconn_ber = ldap_alloc_ber_with_options( ld );
492 if ( lc->lconn_ber == NULL ) {
498 assert( LBER_VALID (ber) );
500 /* get the next message */
502 #ifdef LDAP_CONNECTIONLESS
503 if ( LDAP_IS_UDP(ld) ) {
504 struct sockaddr from;
505 ber_int_sb_read( lc->lconn_sb, &from, sizeof(struct sockaddr) );
506 if ( ld->ld_options.ldo_version == LDAP_VERSION2 ) isv2 = 1;
510 tag = ber_get_next( lc->lconn_sb, &len, ber );
512 case LDAP_TAG_MESSAGE:
514 * We read a complete message.
515 * The connection should no longer need this ber.
517 lc->lconn_ber = NULL;
522 Debug( LDAP_DEBUG_CONNS,
523 "ber_get_next failed.\n", 0, 0, 0 );
526 if ( sock_errno() == EWOULDBLOCK ) return LDAP_MSG_X_KEEP_LOOKING;
529 if ( sock_errno() == EAGAIN ) return LDAP_MSG_X_KEEP_LOOKING;
531 ld->ld_errno = LDAP_SERVER_DOWN;
535 ld->ld_errno = LDAP_LOCAL_ERROR;
540 if ( ber_get_int( ber, &id ) == LBER_ERROR ) {
542 ld->ld_errno = LDAP_DECODING_ERROR;
546 /* if it's been abandoned, toss it */
547 if ( ldap_abandoned( ld, id, &idx ) ) {
548 /* the message type */
549 tag = ber_peek_tag( ber, &len );
551 case LDAP_RES_SEARCH_ENTRY:
552 case LDAP_RES_SEARCH_REFERENCE:
553 case LDAP_RES_INTERMEDIATE:
558 /* there's no need to keep the id
559 * in the abandoned list any longer */
560 ldap_mark_abandoned( ld, id, idx );
564 Debug( LDAP_DEBUG_ANY,
565 "abandoned/discarded ld %p msgid %ld message type %s\n",
566 (void *)ld, (long)id, ldap_int_msgtype2str( tag ) );
570 if ( ber_sockbuf_ctrl( lc->lconn_sb, LBER_SB_OPT_DATA_READY, NULL ) ) {
573 return( LDAP_MSG_X_KEEP_LOOKING ); /* continue looking */
576 lr = ldap_find_request_by_msgid( ld, id );
578 const char *msg = "unknown";
580 /* the message type */
581 tag = ber_peek_tag( ber, &len );
587 msg = ldap_int_msgtype2str( tag );
591 Debug( LDAP_DEBUG_ANY,
592 "no request for response on ld %p msgid %ld message type %s (tossing)\n",
593 (void *)ld, (long)id, msg );
597 #ifdef LDAP_CONNECTIONLESS
598 if ( LDAP_IS_UDP(ld) && isv2 ) {
599 ber_scanf(ber, "x{");
603 /* the message type */
604 tag = ber_peek_tag( ber, &len );
605 if ( tag == LBER_ERROR ) {
606 ld->ld_errno = LDAP_DECODING_ERROR;
611 Debug( LDAP_DEBUG_TRACE,
612 "read1msg: ld %p msgid %ld message type %s\n",
613 (void *)ld, (long)lr->lr_msgid, ldap_int_msgtype2str( tag ) );
617 hadref = simple_request = 0;
618 rc = LDAP_MSG_X_KEEP_LOOKING; /* default is to keep looking (no response found) */
619 lr->lr_res_msgtype = tag;
622 * This code figures out if we are going to chase a
623 * referral / search reference, or pass it back to the application
625 v3ref = V3REF_NOREF; /* Assume not a V3 search reference/referral */
626 if ( tag != LDAP_RES_SEARCH_ENTRY && ld->ld_version > LDAP_VERSION2 ) {
627 BerElement tmpber = *ber; /* struct copy */
630 if ( tag == LDAP_RES_SEARCH_REFERENCE ) {
631 /* This is a V3 search reference */
632 /* Assume we do not chase the reference,
633 * but pass it to application */
635 if ( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ||
636 lr->lr_parent != NULL )
638 /* Get the referral list */
639 if ( ber_scanf( &tmpber, "{v}", &refs ) == LBER_ERROR ) {
640 rc = LDAP_DECODING_ERROR;
643 /* Note: refs array is freed by ldap_chase_v3referrals */
644 refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
645 1, &lr->lr_res_error, &hadref );
646 if ( refer_cnt > 0 ) {
647 /* sucessfully chased reference */
648 /* If haven't got end search, set chasing referrals */
649 if ( lr->lr_status != LDAP_REQST_COMPLETED ) {
650 lr->lr_status = LDAP_REQST_CHASINGREFS;
651 Debug( LDAP_DEBUG_TRACE,
652 "read1msg: search ref chased, "
653 "mark request chasing refs, "
655 lr->lr_msgid, 0, 0 );
658 /* We sucessfully chased the reference */
659 v3ref = V3REF_SUCCESS;
665 /* Check for V3 referral */
667 char *lr_res_error = NULL;
669 if ( ber_scanf( &tmpber, "{eAA",/*}*/ &lderr,
670 &lr->lr_res_matched, &lr_res_error )
673 if ( lr_res_error != NULL ) {
674 if ( lr->lr_res_error != NULL ) {
675 (void)ldap_append_referral( ld, &lr->lr_res_error, lr_res_error );
676 LDAP_FREE( (char *)lr_res_error );
679 lr->lr_res_error = lr_res_error;
684 /* Check if V3 referral */
685 if ( ber_peek_tag( &tmpber, &len ) == LDAP_TAG_REFERRAL ) {
686 /* We have a V3 referral, assume we cannot chase it */
688 if ( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS)
689 || lr->lr_parent != NULL )
691 /* Assume referral not chased and return it to app */
694 /* Get the referral list */
695 if ( ber_scanf( &tmpber, "{v}", &refs) == LBER_ERROR) {
696 rc = LDAP_DECODING_ERROR;
697 lr->lr_status = LDAP_REQST_COMPLETED;
698 Debug( LDAP_DEBUG_TRACE,
699 "read1msg: referral decode error, "
700 "mark request completed, ld %p msgid %d\n",
701 (void *)ld, lr->lr_msgid, 0 );
704 /* Chase the referral
705 * Note: refs arrary is freed by ldap_chase_v3referrals
707 refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
708 0, &lr->lr_res_error, &hadref );
709 lr->lr_status = LDAP_REQST_COMPLETED;
710 Debug( LDAP_DEBUG_TRACE,
711 "read1msg: referral chased, "
712 "mark request completed, ld %p msgid %d\n",
713 (void *)ld, lr->lr_msgid, 0);
714 if ( refer_cnt > 0 ) {
715 /* Referral successfully chased */
716 v3ref = V3REF_SUCCESS;
722 if ( lr->lr_res_matched != NULL ) {
723 LDAP_FREE( lr->lr_res_matched );
724 lr->lr_res_matched = NULL;
727 if ( lr->lr_res_error != NULL ) {
728 LDAP_FREE( lr->lr_res_error );
729 lr->lr_res_error = NULL;
735 /* All results that just return a status, i.e. don't return data
736 * go through the following code. This code also chases V2 referrals
737 * and checks if all referrals have been chased.
739 if ( tag != LDAP_RES_SEARCH_ENTRY &&
740 v3ref != V3REF_TOAPP &&
741 tag != LDAP_RES_INTERMEDIATE )
743 /* For a v3 search referral/reference, only come here if already chased it */
744 if ( ld->ld_version >= LDAP_VERSION2 &&
745 ( lr->lr_parent != NULL ||
746 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ) )
748 char *lr_res_error = NULL;
750 tmpber = *ber; /* struct copy */
751 if ( v3ref == V3REF_SUCCESS ) {
752 /* V3 search reference or V3 referral
753 * sucessfully chased. If this message
754 * is a search result, then it has no more
755 * outstanding referrals.
757 if ( tag == LDAP_RES_SEARCH_RESULT )
760 } else if ( ber_scanf( &tmpber, "{eAA}", &lderr,
761 &lr->lr_res_matched, &lr_res_error )
764 if ( lr_res_error != NULL ) {
765 if ( lr->lr_res_error != NULL ) {
766 (void)ldap_append_referral( ld, &lr->lr_res_error, lr_res_error );
767 LDAP_FREE( (char *)lr_res_error );
769 lr->lr_res_error = lr_res_error;
776 case LDAP_COMPARE_TRUE:
777 case LDAP_COMPARE_FALSE:
781 if ( lr->lr_res_error == NULL ||
782 lr->lr_res_error[ 0 ] == '\0' )
787 /* referrals are in error string */
788 refer_cnt = ldap_chase_referrals( ld, lr,
789 &lr->lr_res_error, -1, &hadref );
790 lr->lr_status = LDAP_REQST_COMPLETED;
791 Debug( LDAP_DEBUG_TRACE,
792 "read1msg: V2 referral chased, "
793 "mark request completed, id = %d\n",
794 lr->lr_msgid, 0, 0 );
798 /* save errno, message, and matched string */
799 if ( !hadref || lr->lr_res_error == NULL ) {
801 lderr == LDAP_PARTIAL_RESULTS
802 ? LDAP_SUCCESS : lderr;
804 } else if ( ld->ld_errno != LDAP_SUCCESS ) {
805 lr->lr_res_errno = ld->ld_errno;
808 lr->lr_res_errno = LDAP_PARTIAL_RESULTS;
811 Debug( LDAP_DEBUG_TRACE, "new result: "
814 "res_matched: <%s>\n",
816 lr->lr_res_error ? lr->lr_res_error : "",
817 lr->lr_res_matched ? lr->lr_res_matched : "" );
820 /* in any case, don't leave any lr_res_error 'round */
821 if ( lr_res_error ) {
822 LDAP_FREE( lr_res_error );
826 Debug( LDAP_DEBUG_TRACE,
827 "read1msg: ld %p %d new referrals\n",
828 (void *)ld, refer_cnt, 0 );
830 if ( refer_cnt != 0 ) { /* chasing referrals */
833 if ( refer_cnt < 0 ) {
834 ldap_return_request( ld, lr, 0 );
835 return( -1 ); /* fatal error */
837 lr->lr_res_errno = LDAP_SUCCESS; /* sucessfully chased referral */
840 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
841 /* request without any referrals */
842 simple_request = ( hadref ? 0 : 1 );
845 /* request with referrals or child request */
850 lr->lr_status = LDAP_REQST_COMPLETED; /* declare this request done */
851 Debug( LDAP_DEBUG_TRACE,
852 "read1msg: mark request completed, ld %p msgid %d\n",
853 (void *)ld, lr->lr_msgid, 0);
854 while ( lr->lr_parent != NULL ) {
855 merge_error_info( ld, lr->lr_parent, lr );
858 if ( --lr->lr_outrefcnt > 0 ) {
859 break; /* not completely done yet */
863 /* Check if all requests are finished, lr is now parent */
865 if ( tmplr->lr_status == LDAP_REQST_COMPLETED ) {
866 for ( tmplr = lr->lr_child;
868 tmplr = tmplr->lr_refnext )
870 if ( tmplr->lr_status != LDAP_REQST_COMPLETED ) break;
874 /* This is the parent request if the request has referrals */
875 if ( lr->lr_outrefcnt <= 0 &&
876 lr->lr_parent == NULL &&
880 tag = lr->lr_res_msgtype;
881 Debug( LDAP_DEBUG_ANY, "request done: ld %p msgid %ld\n",
882 (void *)ld, (long) id, 0 );
883 Debug( LDAP_DEBUG_TRACE,
884 "res_errno: %d, res_error: <%s>, "
885 "res_matched: <%s>\n",
887 lr->lr_res_error ? lr->lr_res_error : "",
888 lr->lr_res_matched ? lr->lr_res_matched : "" );
889 if ( !simple_request ) {
892 if ( build_result_ber( ld, &ber, lr )
895 rc = -1; /* fatal error */
899 ldap_return_request( ld, lr, 1 );
904 #ifdef LDAP_R_COMPILE
905 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
907 ldap_free_connection( ld, lc, 0, 1 );
908 #ifdef LDAP_R_COMPILE
909 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
917 ldap_return_request( ld, lr, 0 );
925 /* make a new ldap message */
926 newmsg = (LDAPMessage *) LDAP_CALLOC( 1, sizeof(LDAPMessage) );
927 if ( newmsg == NULL ) {
928 ld->ld_errno = LDAP_NO_MEMORY;
931 newmsg->lm_msgid = (int)id;
932 newmsg->lm_msgtype = tag;
933 newmsg->lm_ber = ber;
934 newmsg->lm_chain_tail = newmsg;
936 #ifdef LDAP_CONNECTIONLESS
937 /* CLDAP replies all fit in a single datagram. In LDAPv2 RFC1798
938 * the responses are all a sequence wrapped in one message. In
939 * LDAPv3 each response is in its own message. The datagram must
940 * end with a SearchResult. We can't just parse each response in
941 * separate calls to try_read1msg because the header info is only
942 * present at the beginning of the datagram, not at the beginning
943 * of each response. So parse all the responses at once and queue
944 * them up, then pull off the first response to return to the
945 * caller when all parsing is complete.
947 if ( LDAP_IS_UDP(ld) ) {
948 /* If not a result, look for more */
949 if ( tag != LDAP_RES_SEARCH_RESULT ) {
953 /* LDAPv2: dup the current ber, skip past the current
954 * response, and see if there are any more after it.
956 ber = ber_dup( ber );
957 ber_scanf( ber, "x" );
958 if ( ber_peek_tag( ber, &len ) != LBER_DEFAULT ) {
959 /* There's more - dup the ber buffer so they can all be
960 * individually freed by ldap_msgfree.
963 ber_get_option( ber, LBER_OPT_BER_REMAINING_BYTES, &len );
964 bv.bv_val = LDAP_MALLOC( len );
967 ber_read( ber, bv.bv_val, len );
969 ber_init2( ber, &bv, ld->ld_lberoptions );
973 /* LDAPv3: Just allocate a new ber. Since this is a buffered
974 * datagram, if the sockbuf is readable we still have data
977 ber = ldap_alloc_ber_with_options( ld );
978 if ( ber_sockbuf_ctrl( lc->lconn_sb, LBER_SB_OPT_DATA_READY, NULL ) ) ok = 1;
980 /* set up response chain */
982 newmsg->lm_next = ld->ld_responses;
983 ld->ld_responses = newmsg;
986 tmp->lm_chain = newmsg;
988 chain_head->lm_chain_tail = newmsg;
990 /* "ok" means there's more to parse */
999 /* got to end of datagram without a SearchResult. Free
1000 * our dup'd ber, but leave any buffer alone. For v2 case,
1001 * the previous response is still using this buffer. For v3,
1002 * the new ber has no buffer to free yet.
1007 } else if ( moremsgs ) {
1008 /* got search result, and we had multiple responses in 1 datagram.
1009 * stick the result onto the end of the chain, and then pull the
1010 * first response off the head of the chain.
1012 tmp->lm_chain = newmsg;
1013 chain_head->lm_chain_tail = newmsg;
1014 *result = chkResponseList( ld, msgid, all );
1015 ld->ld_errno = LDAP_SUCCESS;
1016 return( (*result)->lm_msgtype );
1019 #endif /* LDAP_CONNECTIONLESS */
1021 /* is this the one we're looking for? */
1022 if ( msgid == LDAP_RES_ANY || id == msgid ) {
1023 if ( all == LDAP_MSG_ONE
1024 || ( newmsg->lm_msgtype != LDAP_RES_SEARCH_RESULT
1025 && newmsg->lm_msgtype != LDAP_RES_SEARCH_ENTRY
1026 && newmsg->lm_msgtype != LDAP_RES_SEARCH_REFERENCE ) )
1029 ld->ld_errno = LDAP_SUCCESS;
1032 } else if ( newmsg->lm_msgtype == LDAP_RES_SEARCH_RESULT) {
1033 foundit = 1; /* return the chain later */
1038 * if not, we must add it to the list of responses. if
1039 * the msgid is already there, it must be part of an existing
1044 for ( l = ld->ld_responses; l != NULL; l = l->lm_next ) {
1045 if ( l->lm_msgid == newmsg->lm_msgid )
1050 /* not part of an existing search response */
1057 newmsg->lm_next = ld->ld_responses;
1058 ld->ld_responses = newmsg;
1062 Debug( LDAP_DEBUG_TRACE, "adding response ld %p msgid %ld type %ld:\n",
1063 (void *)ld, (long) newmsg->lm_msgid, (long) newmsg->lm_msgtype );
1065 /* part of a search response - add to end of list of entries */
1066 l->lm_chain_tail->lm_chain = newmsg;
1067 l->lm_chain_tail = newmsg;
1069 /* return the whole chain if that's what we were looking for */
1072 ld->ld_responses = l->lm_next;
1074 prev->lm_next = l->lm_next;
1080 ld->ld_errno = LDAP_SUCCESS;
1083 if ( lc && ber_sockbuf_ctrl( lc->lconn_sb, LBER_SB_OPT_DATA_READY, NULL ) ) {
1086 return( LDAP_MSG_X_KEEP_LOOKING ); /* continue looking */
1091 build_result_ber( LDAP *ld, BerElement **bp, LDAPRequest *lr )
1099 ber = ldap_alloc_ber_with_options( ld );
1102 ld->ld_errno = LDAP_NO_MEMORY;
1106 if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
1107 lr->lr_res_msgtype, lr->lr_res_errno,
1108 lr->lr_res_matched ? lr->lr_res_matched : "",
1109 lr->lr_res_error ? lr->lr_res_error : "" ) == -1 )
1111 ld->ld_errno = LDAP_ENCODING_ERROR;
1113 return( LBER_ERROR );
1116 ber_reset( ber, 1 );
1118 if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) {
1119 ld->ld_errno = LDAP_DECODING_ERROR;
1121 return( LBER_ERROR );
1124 if ( ber_get_enum( ber, &along ) == LBER_ERROR ) {
1125 ld->ld_errno = LDAP_DECODING_ERROR;
1127 return( LBER_ERROR );
1130 tag = ber_peek_tag( ber, &len );
1132 if ( tag == LBER_ERROR ) {
1133 ld->ld_errno = LDAP_DECODING_ERROR;
1135 return( LBER_ERROR );
1144 * Merge error information in "lr" with "parentr" error code and string.
1147 merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
1149 if ( lr->lr_res_errno == LDAP_PARTIAL_RESULTS ) {
1150 parentr->lr_res_errno = lr->lr_res_errno;
1151 if ( lr->lr_res_error != NULL ) {
1152 (void)ldap_append_referral( ld, &parentr->lr_res_error,
1156 } else if ( lr->lr_res_errno != LDAP_SUCCESS &&
1157 parentr->lr_res_errno == LDAP_SUCCESS )
1159 parentr->lr_res_errno = lr->lr_res_errno;
1160 if ( parentr->lr_res_error != NULL ) {
1161 LDAP_FREE( parentr->lr_res_error );
1163 parentr->lr_res_error = lr->lr_res_error;
1164 lr->lr_res_error = NULL;
1165 if ( LDAP_NAME_ERROR( lr->lr_res_errno ) ) {
1166 if ( parentr->lr_res_matched != NULL ) {
1167 LDAP_FREE( parentr->lr_res_matched );
1169 parentr->lr_res_matched = lr->lr_res_matched;
1170 lr->lr_res_matched = NULL;
1174 Debug( LDAP_DEBUG_TRACE, "merged parent (id %d) error info: ",
1175 parentr->lr_msgid, 0, 0 );
1176 Debug( LDAP_DEBUG_TRACE, "result errno %d, error <%s>, matched <%s>\n",
1177 parentr->lr_res_errno,
1178 parentr->lr_res_error ? parentr->lr_res_error : "",
1179 parentr->lr_res_matched ? parentr->lr_res_matched : "" );
1185 ldap_msgtype( LDAPMessage *lm )
1187 assert( lm != NULL );
1188 return ( lm != NULL ) ? (int)lm->lm_msgtype : -1;
1193 ldap_msgid( LDAPMessage *lm )
1195 assert( lm != NULL );
1197 return ( lm != NULL ) ? lm->lm_msgid : -1;
1202 ldap_int_msgtype2str( ber_tag_t tag )
1205 case LDAP_RES_ADD: return "add";
1206 case LDAP_RES_BIND: return "bind";
1207 case LDAP_RES_COMPARE: return "compare";
1208 case LDAP_RES_DELETE: return "delete";
1209 case LDAP_RES_EXTENDED: return "extended-result";
1210 case LDAP_RES_INTERMEDIATE: return "intermediate";
1211 case LDAP_RES_MODIFY: return "modify";
1212 case LDAP_RES_RENAME: return "rename";
1213 case LDAP_RES_SEARCH_ENTRY: return "search-entry";
1214 case LDAP_RES_SEARCH_REFERENCE: return "search-reference";
1215 case LDAP_RES_SEARCH_RESULT: return "search-result";
1221 ldap_msgfree( LDAPMessage *lm )
1226 Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
1228 for ( ; lm != NULL; lm = next ) {
1229 next = lm->lm_chain;
1230 type = lm->lm_msgtype;
1231 ber_free( lm->lm_ber, 1 );
1232 LDAP_FREE( (char *) lm );
1239 * ldap_msgdelete - delete a message. It returns:
1240 * 0 if the entire message was deleted
1241 * -1 if the message was not found, or only part of it was found
1244 ldap_msgdelete( LDAP *ld, int msgid )
1246 LDAPMessage *lm, *prev;
1249 assert( ld != NULL );
1251 Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete ld=%p msgid=%d\n",
1252 (void *)ld, msgid, 0 );
1254 #ifdef LDAP_R_COMPILE
1255 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
1258 for ( lm = ld->ld_responses; lm != NULL; lm = lm->lm_next ) {
1259 if ( lm->lm_msgid == msgid ) {
1269 if ( prev == NULL ) {
1270 ld->ld_responses = lm->lm_next;
1272 prev->lm_next = lm->lm_next;
1275 #ifdef LDAP_R_COMPILE
1276 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
1279 switch ( ldap_msgfree( lm ) ) {
1280 case LDAP_RES_SEARCH_ENTRY:
1281 case LDAP_RES_SEARCH_REFERENCE:
1282 case LDAP_RES_INTERMEDIATE:
1298 * return the location of the message id in the array of abandoned
1299 * message ids, or -1
1301 * expects ld_res_mutex to be locked
1304 ldap_abandoned( LDAP *ld, ber_int_t msgid, int *idxp )
1306 #ifdef LDAP_R_COMPILE
1307 LDAP_PVT_THREAD_ASSERT_MUTEX_OWNER( &ld->ld_res_mutex );
1310 assert( idxp != NULL );
1311 assert( msgid >= 0 );
1312 assert( ld->ld_nabandoned >= 0 );
1314 return lutil_bisect_find( ld->ld_abandoned, ld->ld_nabandoned, msgid, idxp );
1318 * ldap_mark_abandoned
1320 * expects ld_res_mutex to be locked
1323 ldap_mark_abandoned( LDAP *ld, ber_int_t msgid, int idx )
1327 #ifdef LDAP_R_COMPILE
1328 LDAP_PVT_THREAD_ASSERT_MUTEX_OWNER( &ld->ld_res_mutex );
1331 /* NOTE: those assertions are repeated in lutil_bisect_delete() */
1333 assert( idx < ld->ld_nabandoned );
1334 assert( ld->ld_abandoned[ idx ] == msgid );
1337 fprintf( stderr, "--> ldap_mark_abandoned %p %p %d %d:\n", (void *)&i, (void *)ld, msgid, idx );
1338 for ( i = 0; i < ld->ld_nabandoned; i++ ) {
1339 fprintf( stderr, "\t%p %p %d\n", (void *)&i, (void *)ld, ld->ld_abandoned[ i ] );
1343 return lutil_bisect_delete( &ld->ld_abandoned, &ld->ld_nabandoned, msgid, idx );