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.
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, 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));
81 #define LDAP_MSG_X_KEEP_LOOKING (-2)
85 * ldap_result - wait for an ldap result response to a message from the
86 * ldap server. If msgid is LDAP_RES_ANY (-1), any message will be
87 * accepted. If msgid is LDAP_RES_UNSOLICITED (0), any unsolicited
88 * message is accepted. Otherwise ldap_result will wait for a response
89 * with msgid. If all is LDAP_MSG_ONE (0) the first message with id
90 * msgid will be accepted, otherwise, ldap_result will wait for all
91 * responses with id msgid and then return a pointer to the entire list
92 * of messages. In general, this is only useful for search responses,
93 * which can be of three message types (zero or more entries, zero or
94 * search references, followed by an ldap result). An extension to
95 * LDAPv3 allows partial extended responses to be returned in response
96 * to any request. The type of the first message received is returned.
97 * When waiting, any messages that have been abandoned are discarded.
100 * ldap_result( s, msgid, all, timeout, result )
107 struct timeval *timeout,
108 LDAPMessage **result )
113 assert( ld != NULL );
114 assert( result != NULL );
116 Debug( LDAP_DEBUG_TRACE, "ldap_result ld %p msgid %d\n", (void *)ld, msgid, 0 );
118 #ifdef LDAP_R_COMPILE
119 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
121 lm = chkResponseList(ld, msgid, all);
124 rc = wait4msg( ld, msgid, all, timeout, result );
127 ld->ld_errno = LDAP_SUCCESS;
130 #ifdef LDAP_R_COMPILE
131 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
142 LDAPMessage *lm, **lastlm, *nextlm;
145 * Look through the list of responses we have received on
146 * this association and see if the response we're interested in
147 * is there. If it is, return it. If not, call wait4msg() to
148 * wait until it arrives or timeout occurs.
151 #ifdef LDAP_R_COMPILE
152 LDAP_PVT_THREAD_ASSERT_MUTEX_OWNER( &ld->ld_res_mutex );
155 Debug( LDAP_DEBUG_TRACE,
156 "ldap_chkResponseList ld %p msgid %d all %d\n",
157 (void *)ld, msgid, all );
159 lastlm = &ld->ld_responses;
160 for ( lm = ld->ld_responses; lm != NULL; lm = nextlm ) {
161 nextlm = lm->lm_next;
163 if ( ldap_abandoned( ld, lm->lm_msgid ) ) {
164 Debug( LDAP_DEBUG_TRACE,
165 "ldap_chkResponseList msg abandoned, msgid %d\n",
167 ldap_mark_abandoned( ld, lm->lm_msgid );
169 /* Remove this entry from list */
177 if ( msgid == LDAP_RES_ANY || lm->lm_msgid == msgid ) {
180 if ( all == LDAP_MSG_ONE || all == LDAP_MSG_RECEIVED ||
181 msgid == LDAP_RES_UNSOLICITED ) {
185 tmp = lm->lm_chain_tail;
186 if ((tmp->lm_msgtype == LDAP_RES_SEARCH_ENTRY) ||
187 (tmp->lm_msgtype == LDAP_RES_SEARCH_REFERENCE) ||
188 (tmp->lm_msgtype == LDAP_RES_INTERMEDIATE)) {
198 lastlm = &lm->lm_next;
202 /* Found an entry, remove it from the list */
203 if ( all == LDAP_MSG_ONE && lm->lm_chain != NULL ) {
204 *lastlm = lm->lm_chain;
205 lm->lm_chain->lm_next = lm->lm_next;
206 lm->lm_chain->lm_chain_tail = ( lm->lm_chain_tail != lm ) ? lm->lm_chain_tail : lm->lm_chain;
208 lm->lm_chain_tail = NULL;
210 *lastlm = lm->lm_next;
217 Debug( LDAP_DEBUG_TRACE,
218 "ldap_chkResponseList returns ld %p NULL\n", (void *)ld, 0, 0);
220 Debug( LDAP_DEBUG_TRACE,
221 "ldap_chkResponseList returns ld %p msgid %d, type 0x%02lu\n",
222 (void *)ld, lm->lm_msgid, (unsigned long) lm->lm_msgtype);
233 struct timeval *timeout,
234 LDAPMessage **result )
237 struct timeval tv = { 0 },
240 time_t start_time = 0;
242 LDAPConn *lc, *nextlc;
244 assert( ld != NULL );
245 assert( result != NULL );
247 #ifdef LDAP_R_COMPILE
248 LDAP_PVT_THREAD_ASSERT_MUTEX_OWNER( &ld->ld_res_mutex );
252 if ( timeout == NULL ) {
253 Debug( LDAP_DEBUG_TRACE, "wait4msg ld %p msgid %d (infinite timeout)\n",
254 (void *)ld, msgid, 0 );
256 Debug( LDAP_DEBUG_TRACE, "wait4msg ld %p msgid %d (timeout %ld usec)\n",
257 (void *)ld, msgid, (long)timeout->tv_sec * 1000000 + timeout->tv_usec );
259 #endif /* LDAP_DEBUG */
261 if ( timeout == NULL ) {
267 start_time = time( NULL );
270 rc = LDAP_MSG_X_KEEP_LOOKING;
271 while ( rc == LDAP_MSG_X_KEEP_LOOKING ) {
273 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
274 Debug( LDAP_DEBUG_TRACE, "wait4msg continue ld %p msgid %d all %d\n",
275 (void *)ld, msgid, all );
276 ldap_dump_connection( ld, ld->ld_conns, 1 );
277 ldap_dump_requests_and_responses( ld );
279 #endif /* LDAP_DEBUG */
281 if ( (*result = chkResponseList(ld, msgid, all)) != NULL ) {
282 rc = (*result)->lm_msgtype;
287 #ifdef LDAP_R_COMPILE
288 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
290 for ( lc = ld->ld_conns; lc != NULL; lc = nextlc ) {
291 nextlc = lc->lconn_next;
292 if ( ber_sockbuf_ctrl( lc->lconn_sb,
293 LBER_SB_OPT_DATA_READY, NULL ) ) {
294 #ifdef LDAP_R_COMPILE
295 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
297 rc = try_read1msg( ld, msgid, all, &lc, result );
298 #ifdef LDAP_R_COMPILE
299 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
305 #ifdef LDAP_R_COMPILE
306 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
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 = LDAP_MSG_X_KEEP_LOOKING; /* select interrupted: loop */
331 rc = LDAP_MSG_X_KEEP_LOOKING;
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 );
344 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
346 for ( lc = ld->ld_conns; rc == LDAP_MSG_X_KEEP_LOOKING && lc != NULL;
349 nextlc = lc->lconn_next;
350 if ( lc->lconn_status == LDAP_CONNST_CONNECTED &&
351 ldap_is_read_ready( ld, lc->lconn_sb ))
353 #ifdef LDAP_R_COMPILE
354 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
356 rc = try_read1msg( ld, msgid, all, &lc, result );
357 if ( lc == NULL ) lc = nextlc;
358 #ifdef LDAP_R_COMPILE
359 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
363 #ifdef LDAP_R_COMPILE
364 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
370 if ( rc == LDAP_MSG_X_KEEP_LOOKING && tvp != NULL ) {
371 tmp_time = time( NULL );
372 tv0.tv_sec -= ( tmp_time - start_time );
373 if ( tv0.tv_sec <= 0 ) {
374 rc = 0; /* timed out */
375 ld->ld_errno = LDAP_TIMEOUT;
378 tv.tv_sec = tv0.tv_sec;
380 Debug( LDAP_DEBUG_TRACE, "wait4msg ld %p %ld secs to go\n",
381 (void *)ld, (long) tv.tv_sec, 0 );
382 start_time = tmp_time;
396 LDAPMessage **result )
399 LDAPMessage *newmsg, *l, *prev;
404 LDAPRequest *lr, *tmplr;
407 int rc, refer_cnt, hadref, simple_request;
410 #ifdef LDAP_CONNECTIONLESS
411 LDAPMessage *tmp = NULL, *chain_head = NULL;
412 int moremsgs = 0, isv2 = 0;
416 * v3ref = flag for V3 referral / search reference
417 * 0 = not a ref, 1 = sucessfully chased ref, -1 = pass ref to application
425 assert( ld != NULL );
426 assert( lcp != NULL );
427 assert( *lcp != NULL );
429 #ifdef LDAP_R_COMPILE
430 LDAP_PVT_THREAD_ASSERT_MUTEX_OWNER( &ld->ld_res_mutex );
433 Debug( LDAP_DEBUG_TRACE, "read1msg: ld %p msgid %d all %d\n",
434 (void *)ld, msgid, all );
439 if ( lc->lconn_ber == NULL ) {
440 lc->lconn_ber = ldap_alloc_ber_with_options(ld);
442 if( lc->lconn_ber == NULL ) {
448 assert( LBER_VALID (ber) );
450 /* get the next message */
452 #ifdef LDAP_CONNECTIONLESS
453 if ( LDAP_IS_UDP(ld) ) {
454 struct sockaddr from;
455 ber_int_sb_read( lc->lconn_sb, &from, sizeof(struct sockaddr) );
456 if (ld->ld_options.ldo_version == LDAP_VERSION2) isv2=1;
460 tag = ber_get_next( lc->lconn_sb, &len, ber );
461 if ( tag == LDAP_TAG_MESSAGE ) {
463 * We read a complete message.
464 * The connection should no longer need this ber.
466 lc->lconn_ber = NULL;
468 if ( tag != LDAP_TAG_MESSAGE ) {
469 if ( tag == LBER_DEFAULT) {
471 Debug( LDAP_DEBUG_CONNS,
472 "ber_get_next failed.\n", 0, 0, 0 );
475 if (errno==EWOULDBLOCK) return LDAP_MSG_X_KEEP_LOOKING;
478 if (errno == EAGAIN) return LDAP_MSG_X_KEEP_LOOKING;
480 ld->ld_errno = LDAP_SERVER_DOWN;
483 ld->ld_errno = LDAP_LOCAL_ERROR;
488 if ( ber_get_int( ber, &id ) == LBER_ERROR ) {
490 ld->ld_errno = LDAP_DECODING_ERROR;
494 /* if it's been abandoned, toss it */
495 if ( ldap_abandoned( ld, id ) ) {
496 Debug( LDAP_DEBUG_ANY, "abandoned ld %p msgid %ld\n",
497 (void *)ld, (long) id, 0);
500 if ( ber_sockbuf_ctrl( lc->lconn_sb, LBER_SB_OPT_DATA_READY, NULL ) ) {
503 return( LDAP_MSG_X_KEEP_LOOKING ); /* continue looking */
506 lr = ldap_find_request_by_msgid( ld, id );
508 Debug( LDAP_DEBUG_ANY,
509 "no request for response on ld %p msgid %ld (tossing)\n",
510 (void *)ld, (long) id, 0 );
513 #ifdef LDAP_CONNECTIONLESS
514 if (LDAP_IS_UDP(ld) && isv2) {
515 ber_scanf(ber, "x{");
519 /* the message type */
520 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
521 ld->ld_errno = LDAP_DECODING_ERROR;
526 Debug( LDAP_DEBUG_TRACE,
527 "read1msg: ld %p msgid %ld message type %s\n",
528 (void *)ld, (long) lr->lr_msgid, ldap_int_msgtype2str( tag ));
532 hadref = simple_request = 0;
533 rc = LDAP_MSG_X_KEEP_LOOKING; /* default is to keep looking (no response found) */
534 lr->lr_res_msgtype = tag;
537 * This code figures out if we are going to chase a
538 * referral / search reference, or pass it back to the application
540 v3ref = V3REF_NOREF; /* Assume not a V3 search reference/referral */
541 if( (tag != LDAP_RES_SEARCH_ENTRY) && (ld->ld_version > LDAP_VERSION2) ) {
542 BerElement tmpber = *ber; /* struct copy */
545 if( tag == LDAP_RES_SEARCH_REFERENCE ) {
546 /* This is a V3 search reference */
547 /* Assume we do not chase the reference,
548 * but pass it to application */
550 if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ||
551 (lr->lr_parent != NULL) )
553 /* Get the referral list */
554 if ( ber_scanf( &tmpber, "{v}", &refs ) == LBER_ERROR ) {
555 rc = LDAP_DECODING_ERROR;
557 /* Note: refs array is freed by ldap_chase_v3referrals */
558 refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
559 1, &lr->lr_res_error, &hadref );
560 if ( refer_cnt > 0 ) {
561 /* sucessfully chased reference */
562 /* If haven't got end search, set chasing referrals */
563 if( lr->lr_status != LDAP_REQST_COMPLETED) {
564 lr->lr_status = LDAP_REQST_CHASINGREFS;
565 Debug( LDAP_DEBUG_TRACE,
566 "read1msg: search ref chased, "
567 "mark request chasing refs, "
572 /* We sucessfully chased the reference */
573 v3ref = V3REF_SUCCESS;
578 /* Check for V3 referral */
580 char *lr_res_error = NULL;
582 if ( ber_scanf( &tmpber, "{eAA",/*}*/ &lderr,
583 &lr->lr_res_matched, &lr_res_error )
586 if ( lr_res_error != NULL ) {
588 if ( lr->lr_res_error != NULL ) {
589 (void)ldap_append_referral( ld, &lr->lr_res_error, lr_res_error );
590 LDAP_FREE( (char *)lr_res_error );
593 lr->lr_res_error = lr_res_error;
599 /* Check if V3 referral */
600 if ( ber_peek_tag( &tmpber, &len ) == LDAP_TAG_REFERRAL ) {
601 /* We have a V3 referral, assume we cannot chase it */
603 if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS)
604 || (lr->lr_parent != NULL) )
606 /* Assume referral not chased and return it to app */
609 /* Get the referral list */
610 if( ber_scanf( &tmpber, "{v}", &refs) == LBER_ERROR) {
611 rc = LDAP_DECODING_ERROR;
612 lr->lr_status = LDAP_REQST_COMPLETED;
613 Debug( LDAP_DEBUG_TRACE,
614 "read1msg: referral decode error, mark request completed, ld %p msgid %d\n",
615 (void *)ld, lr->lr_msgid, 0);
617 /* Chase the referral
618 * Note: refs arrary is freed by ldap_chase_v3referrals
620 refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
621 0, &lr->lr_res_error, &hadref );
622 lr->lr_status = LDAP_REQST_COMPLETED;
623 Debug( LDAP_DEBUG_TRACE,
624 "read1msg: referral chased, mark request completed, ld %p msgid %d\n",
625 (void *)ld, lr->lr_msgid, 0);
627 /* Referral successfully chased */
628 v3ref = V3REF_SUCCESS;
634 if( lr->lr_res_matched != NULL ) {
635 LDAP_FREE( lr->lr_res_matched );
636 lr->lr_res_matched = NULL;
638 if( lr->lr_res_error != NULL ) {
639 LDAP_FREE( lr->lr_res_error );
640 lr->lr_res_error = NULL;
646 /* All results that just return a status, i.e. don't return data
647 * go through the following code. This code also chases V2 referrals
648 * and checks if all referrals have been chased.
650 if ( (tag != LDAP_RES_SEARCH_ENTRY) && (v3ref != V3REF_TOAPP) &&
651 (tag != LDAP_RES_INTERMEDIATE ))
653 /* For a v3 search referral/reference, only come here if already chased it */
654 if ( ld->ld_version >= LDAP_VERSION2 &&
655 ( lr->lr_parent != NULL ||
656 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ) )
658 char *lr_res_error = NULL;
660 tmpber = *ber; /* struct copy */
661 if ( v3ref == V3REF_SUCCESS ) {
662 /* V3 search reference or V3 referral
663 * sucessfully chased. If this message
664 * is a search result, then it has no more
665 * outstanding referrals.
667 if ( tag == LDAP_RES_SEARCH_RESULT )
669 } else if ( ber_scanf( &tmpber, "{eAA}", &lderr,
670 &lr->lr_res_matched, &lr_res_error )
673 if ( lr_res_error != NULL ) {
675 if ( lr->lr_res_error != NULL ) {
676 (void)ldap_append_referral( ld, &lr->lr_res_error, lr_res_error );
677 LDAP_FREE( (char *)lr_res_error );
679 lr->lr_res_error = lr_res_error;
687 case LDAP_COMPARE_TRUE:
688 case LDAP_COMPARE_FALSE:
692 if ( lr->lr_res_error == NULL
693 || lr->lr_res_error[ 0 ] == '\0' )
698 /* referrals are in error string */
699 refer_cnt = ldap_chase_referrals( ld, lr,
700 &lr->lr_res_error, -1, &hadref );
701 lr->lr_status = LDAP_REQST_COMPLETED;
702 Debug( LDAP_DEBUG_TRACE,
703 "read1msg: V2 referral chased, "
704 "mark request completed, id = %d\n",
705 lr->lr_msgid, 0, 0 );
709 /* save errno, message, and matched string */
710 if ( !hadref || lr->lr_res_error == NULL ) {
711 lr->lr_res_errno = ( lderr ==
712 LDAP_PARTIAL_RESULTS ) ? LDAP_SUCCESS
714 } else if ( ld->ld_errno != LDAP_SUCCESS ) {
715 lr->lr_res_errno = ld->ld_errno;
717 lr->lr_res_errno = LDAP_PARTIAL_RESULTS;
720 Debug( LDAP_DEBUG_TRACE, "new result: "
723 "res_matched: <%s>\n",
725 lr->lr_res_error ? lr->lr_res_error : "",
726 lr->lr_res_matched ? lr->lr_res_matched : "" );
729 /* in any case, don't leave any lr_res_error 'round */
730 if ( lr_res_error ) {
731 LDAP_FREE( lr_res_error );
735 Debug( LDAP_DEBUG_TRACE,
736 "read1msg: ld %p %d new referrals\n",
737 (void *)ld, refer_cnt, 0 );
739 if ( refer_cnt != 0 ) { /* chasing referrals */
742 if ( refer_cnt < 0 ) {
743 return( -1 ); /* fatal error */
745 lr->lr_res_errno = LDAP_SUCCESS; /* sucessfully chased referral */
747 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
748 /* request without any referrals */
749 simple_request = ( hadref ? 0 : 1 );
751 /* request with referrals or child request */
756 lr->lr_status = LDAP_REQST_COMPLETED; /* declare this request done */
757 Debug( LDAP_DEBUG_TRACE,
758 "read1msg: mark request completed, ld %p msgid %d\n",
759 (void *)ld, lr->lr_msgid, 0);
760 while ( lr->lr_parent != NULL ) {
761 merge_error_info( ld, lr->lr_parent, lr );
764 if ( --lr->lr_outrefcnt > 0 ) {
765 break; /* not completely done yet */
769 /* Check if all requests are finished, lr is now parent */
771 if (tmplr->lr_status == LDAP_REQST_COMPLETED) {
772 for ( tmplr=lr->lr_child;
774 tmplr=tmplr->lr_refnext)
776 if( tmplr->lr_status != LDAP_REQST_COMPLETED) break;
780 /* This is the parent request if the request has referrals */
781 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL &&
785 tag = lr->lr_res_msgtype;
786 Debug( LDAP_DEBUG_ANY, "request done: ld %p msgid %ld\n",
787 (void *)ld, (long) id, 0 );
788 Debug( LDAP_DEBUG_TRACE,
789 "res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
790 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
791 lr->lr_res_matched ? lr->lr_res_matched : "" );
792 if ( !simple_request ) {
795 if ( build_result_ber( ld, &ber, lr )
797 rc = -1; /* fatal error */
801 #ifdef LDAP_R_COMPILE
802 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
804 ldap_free_request( ld, lr );
805 #ifdef LDAP_R_COMPILE
806 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
811 #ifdef LDAP_R_COMPILE
812 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
814 ldap_free_connection( ld, lc, 0, 1 );
815 #ifdef LDAP_R_COMPILE
816 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
827 /* make a new ldap message */
828 newmsg = (LDAPMessage *) LDAP_CALLOC( 1, sizeof(LDAPMessage) );
829 if ( newmsg == NULL ) {
830 ld->ld_errno = LDAP_NO_MEMORY;
833 newmsg->lm_msgid = (int)id;
834 newmsg->lm_msgtype = tag;
835 newmsg->lm_ber = ber;
836 newmsg->lm_chain_tail = newmsg;
838 #ifdef LDAP_CONNECTIONLESS
839 /* CLDAP replies all fit in a single datagram. In LDAPv2 RFC1798
840 * the responses are all a sequence wrapped in one message. In
841 * LDAPv3 each response is in its own message. The datagram must
842 * end with a SearchResult. We can't just parse each response in
843 * separate calls to try_read1msg because the header info is only
844 * present at the beginning of the datagram, not at the beginning
845 * of each response. So parse all the responses at once and queue
846 * them up, then pull off the first response to return to the
847 * caller when all parsing is complete.
849 if ( LDAP_IS_UDP(ld) ) {
850 /* If not a result, look for more */
851 if ( tag != LDAP_RES_SEARCH_RESULT ) {
855 /* LDAPv2: dup the current ber, skip past the current
856 * response, and see if there are any more after it.
858 ber = ber_dup( ber );
859 ber_scanf( ber, "x" );
860 if (ber_peek_tag(ber, &len) != LBER_DEFAULT) {
861 /* There's more - dup the ber buffer so they can all be
862 * individually freed by ldap_msgfree.
865 ber_get_option(ber, LBER_OPT_BER_REMAINING_BYTES, &len);
866 bv.bv_val = LDAP_MALLOC(len);
869 ber_read(ber, bv.bv_val, len);
871 ber_init2(ber, &bv, ld->ld_lberoptions );
875 /* LDAPv3: Just allocate a new ber. Since this is a buffered
876 * datagram, if the sockbuf is readable we still have data
879 ber = ldap_alloc_ber_with_options( ld );
880 if ( ber_sockbuf_ctrl( lc->lconn_sb, LBER_SB_OPT_DATA_READY, NULL ) ) ok = 1;
882 /* set up response chain */
884 newmsg->lm_next = ld->ld_responses;
885 ld->ld_responses = newmsg;
888 tmp->lm_chain = newmsg;
890 chain_head->lm_chain_tail = newmsg;
892 /* "ok" means there's more to parse */
894 if (isv2) goto nextresp2;
897 /* got to end of datagram without a SearchResult. Free
898 * our dup'd ber, but leave any buffer alone. For v2 case,
899 * the previous response is still using this buffer. For v3,
900 * the new ber has no buffer to free yet.
905 } else if ( moremsgs ) {
906 /* got search result, and we had multiple responses in 1 datagram.
907 * stick the result onto the end of the chain, and then pull the
908 * first response off the head of the chain.
910 tmp->lm_chain = newmsg;
911 chain_head->lm_chain_tail = newmsg;
912 *result = chkResponseList( ld, msgid, all );
913 ld->ld_errno = LDAP_SUCCESS;
914 return( (*result)->lm_msgtype );
917 #endif /* LDAP_CONNECTIONLESS */
919 /* is this the one we're looking for? */
920 if ( msgid == LDAP_RES_ANY || id == msgid ) {
921 if ( all == LDAP_MSG_ONE
922 || (newmsg->lm_msgtype != LDAP_RES_SEARCH_RESULT
923 && newmsg->lm_msgtype != LDAP_RES_SEARCH_ENTRY
924 && newmsg->lm_msgtype != LDAP_RES_SEARCH_REFERENCE) ) {
926 ld->ld_errno = LDAP_SUCCESS;
928 } else if ( newmsg->lm_msgtype == LDAP_RES_SEARCH_RESULT) {
929 foundit = 1; /* return the chain later */
934 * if not, we must add it to the list of responses. if
935 * the msgid is already there, it must be part of an existing
940 for ( l = ld->ld_responses; l != NULL; l = l->lm_next ) {
941 if ( l->lm_msgid == newmsg->lm_msgid )
946 /* not part of an existing search response */
953 newmsg->lm_next = ld->ld_responses;
954 ld->ld_responses = newmsg;
958 Debug( LDAP_DEBUG_TRACE, "adding response ld %p msgid %ld type %ld:\n",
959 (void *)ld, (long) newmsg->lm_msgid, (long) newmsg->lm_msgtype );
961 /* part of a search response - add to end of list of entries */
962 l->lm_chain_tail->lm_chain = newmsg;
963 l->lm_chain_tail = newmsg;
965 /* return the whole chain if that's what we were looking for */
968 ld->ld_responses = l->lm_next;
970 prev->lm_next = l->lm_next;
976 ld->ld_errno = LDAP_SUCCESS;
979 if ( lc && ber_sockbuf_ctrl( lc->lconn_sb, LBER_SB_OPT_DATA_READY, NULL ) ) {
982 return( LDAP_MSG_X_KEEP_LOOKING ); /* continue looking */
987 build_result_ber( LDAP *ld, BerElement **bp, LDAPRequest *lr )
995 ber = ldap_alloc_ber_with_options( ld );
998 ld->ld_errno = LDAP_NO_MEMORY;
1002 if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
1003 lr->lr_res_msgtype, lr->lr_res_errno,
1004 lr->lr_res_matched ? lr->lr_res_matched : "",
1005 lr->lr_res_error ? lr->lr_res_error : "" ) == -1 )
1007 ld->ld_errno = LDAP_ENCODING_ERROR;
1009 return( LBER_ERROR );
1012 ber_reset( ber, 1 );
1014 if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) {
1015 ld->ld_errno = LDAP_DECODING_ERROR;
1017 return( LBER_ERROR );
1020 if ( ber_get_enum( ber, &along ) == LBER_ERROR ) {
1021 ld->ld_errno = LDAP_DECODING_ERROR;
1023 return( LBER_ERROR );
1026 tag = ber_peek_tag( ber, &len );
1028 if ( tag == LBER_ERROR ) {
1029 ld->ld_errno = LDAP_DECODING_ERROR;
1031 return( LBER_ERROR );
1040 merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
1043 * Merge error information in "lr" with "parentr" error code and string.
1045 if ( lr->lr_res_errno == LDAP_PARTIAL_RESULTS ) {
1046 parentr->lr_res_errno = lr->lr_res_errno;
1047 if ( lr->lr_res_error != NULL ) {
1048 (void)ldap_append_referral( ld, &parentr->lr_res_error,
1051 } else if ( lr->lr_res_errno != LDAP_SUCCESS &&
1052 parentr->lr_res_errno == LDAP_SUCCESS )
1054 parentr->lr_res_errno = lr->lr_res_errno;
1055 if ( parentr->lr_res_error != NULL ) {
1056 LDAP_FREE( parentr->lr_res_error );
1058 parentr->lr_res_error = lr->lr_res_error;
1059 lr->lr_res_error = NULL;
1060 if ( LDAP_NAME_ERROR( lr->lr_res_errno ) ) {
1061 if ( parentr->lr_res_matched != NULL ) {
1062 LDAP_FREE( parentr->lr_res_matched );
1064 parentr->lr_res_matched = lr->lr_res_matched;
1065 lr->lr_res_matched = NULL;
1069 Debug( LDAP_DEBUG_TRACE, "merged parent (id %d) error info: ",
1070 parentr->lr_msgid, 0, 0 );
1071 Debug( LDAP_DEBUG_TRACE, "result errno %d, error <%s>, matched <%s>\n",
1072 parentr->lr_res_errno, parentr->lr_res_error ?
1073 parentr->lr_res_error : "", parentr->lr_res_matched ?
1074 parentr->lr_res_matched : "" );
1080 ldap_msgtype( LDAPMessage *lm )
1082 assert( lm != NULL );
1083 return ( lm != NULL ) ? (int)lm->lm_msgtype : -1;
1088 ldap_msgid( LDAPMessage *lm )
1090 assert( lm != NULL );
1092 return ( lm != NULL ) ? lm->lm_msgid : -1;
1096 char * ldap_int_msgtype2str( ber_tag_t tag )
1099 case LDAP_RES_ADD: return "add";
1100 case LDAP_RES_BIND: return "bind";
1101 case LDAP_RES_COMPARE: return "compare";
1102 case LDAP_RES_DELETE: return "delete";
1103 case LDAP_RES_EXTENDED: return "extended-result";
1104 case LDAP_RES_INTERMEDIATE: return "intermediate";
1105 case LDAP_RES_MODIFY: return "modify";
1106 case LDAP_RES_RENAME: return "rename";
1107 case LDAP_RES_SEARCH_ENTRY: return "search-entry";
1108 case LDAP_RES_SEARCH_REFERENCE: return "search-reference";
1109 case LDAP_RES_SEARCH_RESULT: return "search-result";
1115 ldap_msgfree( LDAPMessage *lm )
1120 Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
1122 for ( ; lm != NULL; lm = next ) {
1123 next = lm->lm_chain;
1124 type = lm->lm_msgtype;
1125 ber_free( lm->lm_ber, 1 );
1126 LDAP_FREE( (char *) lm );
1133 * ldap_msgdelete - delete a message. It returns:
1134 * 0 if the entire message was deleted
1135 * -1 if the message was not found, or only part of it was found
1138 ldap_msgdelete( LDAP *ld, int msgid )
1140 LDAPMessage *lm, *prev;
1143 assert( ld != NULL );
1145 Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete\n", 0, 0, 0 );
1148 #ifdef LDAP_R_COMPILE
1149 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
1151 for ( lm = ld->ld_responses; lm != NULL; lm = lm->lm_next ) {
1152 if ( lm->lm_msgid == msgid )
1161 ld->ld_responses = lm->lm_next;
1163 prev->lm_next = lm->lm_next;
1165 #ifdef LDAP_R_COMPILE
1166 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
1168 if ( lm && ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY )
1178 * return 1 if message msgid is waiting to be abandoned, 0 otherwise
1180 * expects ld_res_mutex to be locked
1183 ldap_abandoned( LDAP *ld, ber_int_t msgid )
1187 #ifdef LDAP_R_COMPILE
1188 LDAP_PVT_THREAD_ASSERT_MUTEX_OWNER( &ld->ld_res_mutex );
1191 if ( ld->ld_abandoned == NULL )
1194 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
1195 if ( ld->ld_abandoned[i] == msgid )
1203 * ldap_mark_abandoned
1205 * expects ld_res_mutex to be locked
1208 ldap_mark_abandoned( LDAP *ld, ber_int_t msgid )
1212 #ifdef LDAP_R_COMPILE
1213 LDAP_PVT_THREAD_ASSERT_MUTEX_OWNER( &ld->ld_res_mutex );
1216 if ( ld->ld_abandoned == NULL )
1219 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
1220 if ( ld->ld_abandoned[i] == msgid )
1223 if ( ld->ld_abandoned[i] == -1 )
1226 for ( ; ld->ld_abandoned[i] != -1; i++ ) {
1227 ld->ld_abandoned[i] = ld->ld_abandoned[i + 1];