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 ld %p msgid %d\n", ld, msgid, 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 ld %p msgid %d all %d\n",
151 lastlm = &ld->ld_responses;
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 /* Remove this entry from list */
169 if ( msgid == LDAP_RES_ANY || lm->lm_msgid == msgid ) {
172 if ( all == LDAP_MSG_ONE || all == LDAP_MSG_RECEIVED ||
173 msgid == LDAP_RES_UNSOLICITED ) {
177 tmp = lm->lm_chain_tail;
178 if ((tmp->lm_msgtype == LDAP_RES_SEARCH_ENTRY) ||
179 (tmp->lm_msgtype == LDAP_RES_SEARCH_REFERENCE) ||
180 (tmp->lm_msgtype == LDAP_RES_INTERMEDIATE)) {
190 lastlm = &lm->lm_next;
194 /* Found an entry, remove it from the list */
195 if ( all == LDAP_MSG_ONE && lm->lm_chain != NULL ) {
196 *lastlm = lm->lm_chain;
197 lm->lm_chain->lm_next = lm->lm_next;
198 lm->lm_chain->lm_chain_tail = ( lm->lm_chain_tail != lm ) ? lm->lm_chain_tail : lm->lm_chain;
200 lm->lm_chain_tail = NULL;
202 *lastlm = lm->lm_next;
209 Debug( LDAP_DEBUG_TRACE,
210 "ldap_chkResponseList returns ld %p NULL\n", ld, 0, 0);
212 Debug( LDAP_DEBUG_TRACE,
213 "ldap_chkResponseList returns ld %p msgid %d, type 0x%02lu\n",
214 ld, lm->lm_msgid, (unsigned long) lm->lm_msgtype);
225 struct timeval *timeout,
226 LDAPMessage **result )
229 struct timeval tv = { 0 },
232 time_t start_time = 0;
234 LDAPConn *lc, *nextlc;
236 assert( ld != NULL );
237 assert( result != NULL );
240 if ( timeout == NULL ) {
241 Debug( LDAP_DEBUG_TRACE, "wait4msg ld %p msgid %d (infinite timeout)\n",
244 Debug( LDAP_DEBUG_TRACE, "wait4msg ld %p msgid %d (timeout %ld usec)\n",
245 ld, (long)timeout->tv_sec * 1000000 + timeout->tv_usec, msgid );
247 #endif /* LDAP_DEBUG */
249 if ( timeout == NULL ) {
255 start_time = time( NULL );
261 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
262 Debug( LDAP_DEBUG_TRACE, "wait4msg continue ld %p msgid %d all %d\n",
264 ldap_dump_connection( ld, ld->ld_conns, 1 );
265 ldap_dump_requests_and_responses( ld );
267 #endif /* LDAP_DEBUG */
269 if ( (*result = chkResponseList(ld, msgid, all)) != NULL ) {
270 rc = (*result)->lm_msgtype;
275 #ifdef LDAP_R_COMPILE
276 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
278 for ( lc = ld->ld_conns; lc != NULL; lc = nextlc ) {
279 nextlc = lc->lconn_next;
280 if ( ber_sockbuf_ctrl( lc->lconn_sb,
281 LBER_SB_OPT_DATA_READY, NULL ) ) {
282 #ifdef LDAP_R_COMPILE
283 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
285 rc = try_read1msg( ld, msgid, all, lc->lconn_sb,
287 #ifdef LDAP_R_COMPILE
288 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
294 #ifdef LDAP_R_COMPILE
295 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
299 rc = ldap_int_select( ld, tvp );
302 Debug( LDAP_DEBUG_TRACE,
303 "ldap_int_select returned -1: errno %d\n",
308 if ( rc == 0 || ( rc == -1 && (
309 !LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART)
310 || errno != EINTR )))
312 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
318 rc = -2; /* select interrupted: loop */
321 #ifdef LDAP_R_COMPILE
322 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
324 if ( ld->ld_requests &&
325 ld->ld_requests->lr_status == LDAP_REQST_WRITING &&
326 ldap_is_write_ready( ld,
327 ld->ld_requests->lr_conn->lconn_sb ) )
329 ldap_int_flush_request( ld, ld->ld_requests );
331 #ifdef LDAP_R_COMPILE
332 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
333 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
335 for ( lc = ld->ld_conns; rc == -2 && lc != NULL;
338 nextlc = lc->lconn_next;
339 if ( lc->lconn_status == LDAP_CONNST_CONNECTED &&
340 ldap_is_read_ready( ld, lc->lconn_sb ))
342 #ifdef LDAP_R_COMPILE
343 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
345 rc = try_read1msg( ld, msgid, all,
346 lc->lconn_sb, &lc, result );
347 if ( lc == NULL ) lc = nextlc;
348 #ifdef LDAP_R_COMPILE
349 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
353 #ifdef LDAP_R_COMPILE
354 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
360 if ( rc == -2 && tvp != NULL ) {
361 tmp_time = time( NULL );
362 tv0.tv_sec -= ( tmp_time - start_time );
363 if ( tv0.tv_sec <= 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 %p %ld secs to go\n",
371 ld, (long) tv.tv_sec, 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 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
416 assert( ld != NULL );
417 assert( lcp != NULL );
418 assert( *lcp != NULL );
420 Debug( LDAP_DEBUG_TRACE, "read1msg: ld %p msgid %d all %d\n", ld, msgid, all );
425 if ( lc->lconn_ber == NULL ) {
426 lc->lconn_ber = ldap_alloc_ber_with_options(ld);
428 if( lc->lconn_ber == NULL ) {
434 assert( LBER_VALID (ber) );
436 /* get the next message */
438 #ifdef LDAP_CONNECTIONLESS
439 if ( LDAP_IS_UDP(ld) ) {
440 struct sockaddr from;
441 ber_int_sb_read(sb, &from, sizeof(struct sockaddr));
442 if (ld->ld_options.ldo_version == LDAP_VERSION2) isv2=1;
446 tag = ber_get_next( sb, &len, ber );
447 if ( tag == LDAP_TAG_MESSAGE ) {
449 * We read a complete message.
450 * The connection should no longer need this ber.
452 lc->lconn_ber = NULL;
454 if ( tag != LDAP_TAG_MESSAGE ) {
455 if ( tag == LBER_DEFAULT) {
457 Debug( LDAP_DEBUG_CONNS,
458 "ber_get_next failed.\n", 0, 0, 0 );
461 if (errno==EWOULDBLOCK) return -2;
464 if (errno == EAGAIN) return -2;
466 ld->ld_errno = LDAP_SERVER_DOWN;
469 ld->ld_errno = LDAP_LOCAL_ERROR;
474 if ( ber_get_int( ber, &id ) == LBER_ERROR ) {
476 ld->ld_errno = LDAP_DECODING_ERROR;
480 /* if it's been abandoned, toss it */
481 if ( ldap_abandoned( ld, id ) ) {
482 Debug( LDAP_DEBUG_ANY, "abandoned ld %p msgid %ld\n",
486 if ( ber_sockbuf_ctrl( sb, LBER_SB_OPT_DATA_READY, NULL ) ) {
489 return( -2 ); /* continue looking */
492 lr = ldap_find_request_by_msgid( ld, id );
494 Debug( LDAP_DEBUG_ANY,
495 "no request for response on ld %p msgid %ld (tossing)\n",
499 #ifdef LDAP_CONNECTIONLESS
500 if (LDAP_IS_UDP(ld) && isv2) {
501 ber_scanf(ber, "x{");
505 /* the message type */
506 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
507 ld->ld_errno = LDAP_DECODING_ERROR;
512 Debug( LDAP_DEBUG_TRACE,
513 "read1msg: ld %p msgid %ld message type %s\n",
514 ld, (long) lr->lr_msgid, ldap_int_msgtype2str( tag ));
518 hadref = simple_request = 0;
519 rc = -2; /* default is to keep looking (no response found) */
520 lr->lr_res_msgtype = tag;
523 * This code figures out if we are going to chase a
524 * referral / search reference, or pass it back to the application
526 v3ref = V3REF_NOREF; /* Assume not a V3 search reference/referral */
527 if( (tag != LDAP_RES_SEARCH_ENTRY) && (ld->ld_version > LDAP_VERSION2) ) {
528 BerElement tmpber = *ber; /* struct copy */
531 if( tag == LDAP_RES_SEARCH_REFERENCE ) {
532 /* This is a V3 search reference */
533 /* Assume we do not chase the reference,
534 * but pass it to application */
536 if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ||
537 (lr->lr_parent != NULL) )
539 /* Get the referral list */
540 if ( ber_scanf( &tmpber, "{v}", &refs ) == LBER_ERROR ) {
541 rc = LDAP_DECODING_ERROR;
543 /* Note: refs array is freed by ldap_chase_v3referrals */
544 refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
545 1, &lr->lr_res_error, &hadref );
546 if ( refer_cnt > 0 ) {
547 /* sucessfully chased reference */
548 /* If haven't got end search, set chasing referrals */
549 if( lr->lr_status != LDAP_REQST_COMPLETED) {
550 lr->lr_status = LDAP_REQST_CHASINGREFS;
551 Debug( LDAP_DEBUG_TRACE,
552 "read1msg: search ref chased, "
553 "mark request chasing refs, "
558 /* We sucessfully chased the reference */
559 v3ref = V3REF_SUCCESS;
564 /* Check for V3 referral */
566 char *lr_res_error = NULL;
568 if ( ber_scanf( &tmpber, "{eaa",/*}*/ &lderr,
569 &lr->lr_res_matched, &lr_res_error )
572 if ( lr_res_error != NULL ) {
573 if ( lr->lr_res_error != NULL ) {
574 (void)ldap_append_referral( ld, &lr->lr_res_error, lr_res_error );
575 LDAP_FREE( (char *)lr_res_error );
578 lr->lr_res_error = lr_res_error;
582 /* Check if V3 referral */
583 if ( ber_peek_tag( &tmpber, &len ) == LDAP_TAG_REFERRAL ) {
584 /* We have a V3 referral, assume we cannot chase it */
586 if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS)
587 || (lr->lr_parent != NULL) )
589 /* Assume referral not chased and return it to app */
592 /* Get the referral list */
593 if( ber_scanf( &tmpber, "{v}", &refs) == LBER_ERROR) {
594 rc = LDAP_DECODING_ERROR;
595 lr->lr_status = LDAP_REQST_COMPLETED;
596 Debug( LDAP_DEBUG_TRACE,
597 "read1msg: referral decode error, mark request completed, ld %p msgid %d\n",
598 ld, lr->lr_msgid, 0);
600 /* Chase the referral
601 * Note: refs arrary is freed by ldap_chase_v3referrals
603 refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
604 0, &lr->lr_res_error, &hadref );
605 lr->lr_status = LDAP_REQST_COMPLETED;
606 Debug( LDAP_DEBUG_TRACE,
607 "read1msg: referral chased, mark request completed, ld %p msgid %d\n",
608 ld, lr->lr_msgid, 0);
610 /* Referral successfully chased */
611 v3ref = V3REF_SUCCESS;
617 if( lr->lr_res_matched != NULL ) {
618 LDAP_FREE( lr->lr_res_matched );
619 lr->lr_res_matched = NULL;
621 if( lr->lr_res_error != NULL ) {
622 LDAP_FREE( lr->lr_res_error );
623 lr->lr_res_error = NULL;
629 /* All results that just return a status, i.e. don't return data
630 * go through the following code. This code also chases V2 referrals
631 * and checks if all referrals have been chased.
633 if ( (tag != LDAP_RES_SEARCH_ENTRY) && (v3ref != V3REF_TOAPP) &&
634 (tag != LDAP_RES_INTERMEDIATE ))
636 /* For a v3 search referral/reference, only come here if already chased it */
637 if ( ld->ld_version >= LDAP_VERSION2 &&
638 ( lr->lr_parent != NULL ||
639 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ) )
641 char *lr_res_error = NULL;
643 tmpber = *ber; /* struct copy */
644 if ( v3ref == V3REF_SUCCESS ) {
645 /* V3 search reference or V3 referral
646 * sucessfully chased. If this message
647 * is a search result, then it has no more
648 * outstanding referrals.
650 if ( tag == LDAP_RES_SEARCH_RESULT )
652 } else if ( ber_scanf( &tmpber, "{eaa}", &lderr,
653 &lr->lr_res_matched, &lr_res_error )
656 if ( lr_res_error != NULL ) {
657 if ( lr->lr_res_error != NULL ) {
658 (void)ldap_append_referral( ld, &lr->lr_res_error, lr_res_error );
659 LDAP_FREE( (char *)lr_res_error );
661 lr->lr_res_error = lr_res_error;
668 case LDAP_COMPARE_TRUE:
669 case LDAP_COMPARE_FALSE:
673 if ( lr->lr_res_error == NULL
674 || lr->lr_res_error[ 0 ] == '\0' )
679 /* referrals are in error string */
680 refer_cnt = ldap_chase_referrals( ld, lr,
681 &lr->lr_res_error, -1, &hadref );
682 lr->lr_status = LDAP_REQST_COMPLETED;
683 Debug( LDAP_DEBUG_TRACE,
684 "read1msg: V2 referral chased, "
685 "mark request completed, id = %d\n",
686 lr->lr_msgid, 0, 0 );
690 /* save errno, message, and matched string */
691 if ( !hadref || lr->lr_res_error == NULL ) {
692 lr->lr_res_errno = ( lderr ==
693 LDAP_PARTIAL_RESULTS ) ? LDAP_SUCCESS
695 } else if ( ld->ld_errno != LDAP_SUCCESS ) {
696 lr->lr_res_errno = ld->ld_errno;
698 lr->lr_res_errno = LDAP_PARTIAL_RESULTS;
700 Debug( LDAP_DEBUG_TRACE,
701 "new result: res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
702 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
703 lr->lr_res_matched ? lr->lr_res_matched : "" );
707 Debug( LDAP_DEBUG_TRACE,
708 "read1msg: ld %p %d new referrals\n", ld, refer_cnt, 0 );
710 if ( refer_cnt != 0 ) { /* chasing referrals */
713 if ( refer_cnt < 0 ) {
714 return( -1 ); /* fatal error */
716 lr->lr_res_errno = LDAP_SUCCESS; /* sucessfully chased referral */
718 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
719 /* request without any referrals */
720 simple_request = ( hadref ? 0 : 1 );
722 /* request with referrals or child request */
727 lr->lr_status = LDAP_REQST_COMPLETED; /* declare this request done */
728 Debug( LDAP_DEBUG_TRACE,
729 "read1msg: mark request completed, ld %p msgid %d\n", ld, lr->lr_msgid, 0);
730 while ( lr->lr_parent != NULL ) {
731 merge_error_info( ld, lr->lr_parent, lr );
734 if ( --lr->lr_outrefcnt > 0 ) {
735 break; /* not completely done yet */
739 /* Check if all requests are finished, lr is now parent */
741 if (tmplr->lr_status == LDAP_REQST_COMPLETED) {
742 for ( tmplr=lr->lr_child;
744 tmplr=tmplr->lr_refnext)
746 if( tmplr->lr_status != LDAP_REQST_COMPLETED) break;
750 /* This is the parent request if the request has referrals */
751 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL &&
755 tag = lr->lr_res_msgtype;
756 Debug( LDAP_DEBUG_ANY, "request done: ld %p msgid %ld\n",
758 Debug( LDAP_DEBUG_TRACE,
759 "res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
760 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
761 lr->lr_res_matched ? lr->lr_res_matched : "" );
762 if ( !simple_request ) {
765 if ( build_result_ber( ld, &ber, lr )
767 rc = -1; /* fatal error */
771 #ifdef LDAP_R_COMPILE
772 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
774 ldap_free_request( ld, lr );
775 #ifdef LDAP_R_COMPILE
776 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
781 #ifdef LDAP_R_COMPILE
782 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
784 ldap_free_connection( ld, lc, 0, 1 );
785 #ifdef LDAP_R_COMPILE
786 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
797 /* make a new ldap message */
798 newmsg = (LDAPMessage *) LDAP_CALLOC( 1, sizeof(LDAPMessage) );
799 if ( newmsg == NULL ) {
800 ld->ld_errno = LDAP_NO_MEMORY;
803 newmsg->lm_msgid = (int)id;
804 newmsg->lm_msgtype = tag;
805 newmsg->lm_ber = ber;
806 newmsg->lm_chain_tail = newmsg;
808 #ifdef LDAP_CONNECTIONLESS
809 /* CLDAP replies all fit in a single datagram. In LDAPv2 RFC1798
810 * the responses are all a sequence wrapped in one message. In
811 * LDAPv3 each response is in its own message. The datagram must
812 * end with a SearchResult. We can't just parse each response in
813 * separate calls to try_read1msg because the header info is only
814 * present at the beginning of the datagram, not at the beginning
815 * of each response. So parse all the responses at once and queue
816 * them up, then pull off the first response to return to the
817 * caller when all parsing is complete.
819 if ( LDAP_IS_UDP(ld) ) {
820 /* If not a result, look for more */
821 if ( tag != LDAP_RES_SEARCH_RESULT ) {
825 /* LDAPv2: dup the current ber, skip past the current
826 * response, and see if there are any more after it.
828 ber = ber_dup( ber );
829 ber_scanf( ber, "x" );
830 if (ber_peek_tag(ber, &len) != LBER_DEFAULT) {
831 /* There's more - dup the ber buffer so they can all be
832 * individually freed by ldap_msgfree.
835 ber_get_option(ber, LBER_OPT_BER_REMAINING_BYTES, &len);
836 bv.bv_val = LDAP_MALLOC(len);
839 ber_read(ber, bv.bv_val, len);
841 ber_init2(ber, &bv, ld->ld_lberoptions );
845 /* LDAPv3: Just allocate a new ber. Since this is a buffered
846 * datagram, if the sockbuf is readable we still have data
849 ber = ldap_alloc_ber_with_options(ld);
850 if (ber_sockbuf_ctrl(sb, LBER_SB_OPT_DATA_READY, NULL)) ok=1;
852 /* set up response chain */
854 newmsg->lm_next = ld->ld_responses;
855 ld->ld_responses = newmsg;
858 tmp->lm_chain = newmsg;
860 chain_head->lm_chain_tail = newmsg;
862 /* "ok" means there's more to parse */
864 if (isv2) goto nextresp2;
867 /* got to end of datagram without a SearchResult. Free
868 * our dup'd ber, but leave any buffer alone. For v2 case,
869 * the previous response is still using this buffer. For v3,
870 * the new ber has no buffer to free yet.
875 } else if ( moremsgs ) {
876 /* got search result, and we had multiple responses in 1 datagram.
877 * stick the result onto the end of the chain, and then pull the
878 * first response off the head of the chain.
880 tmp->lm_chain = newmsg;
881 chain_head->lm_chain_tail = newmsg;
882 *result = chkResponseList( ld, msgid, all );
883 ld->ld_errno = LDAP_SUCCESS;
884 return( (*result)->lm_msgtype );
887 #endif /* LDAP_CONNECTIONLESS */
889 /* is this the one we're looking for? */
890 if ( msgid == LDAP_RES_ANY || id == msgid ) {
891 if ( all == LDAP_MSG_ONE
892 || (newmsg->lm_msgtype != LDAP_RES_SEARCH_RESULT
893 && newmsg->lm_msgtype != LDAP_RES_SEARCH_ENTRY
894 && newmsg->lm_msgtype != LDAP_RES_SEARCH_REFERENCE) ) {
896 ld->ld_errno = LDAP_SUCCESS;
898 } else if ( newmsg->lm_msgtype == LDAP_RES_SEARCH_RESULT) {
899 foundit = 1; /* return the chain later */
904 * if not, we must add it to the list of responses. if
905 * the msgid is already there, it must be part of an existing
910 for ( l = ld->ld_responses; l != NULL; l = l->lm_next ) {
911 if ( l->lm_msgid == newmsg->lm_msgid )
916 /* not part of an existing search response */
923 newmsg->lm_next = ld->ld_responses;
924 ld->ld_responses = newmsg;
928 Debug( LDAP_DEBUG_TRACE, "adding response ld %p msgid %ld type %ld:\n",
929 ld, (long) newmsg->lm_msgid, (long) newmsg->lm_msgtype );
931 /* part of a search response - add to end of list of entries */
932 l->lm_chain_tail->lm_chain = newmsg;
933 l->lm_chain_tail = newmsg;
935 /* return the whole chain if that's what we were looking for */
938 ld->ld_responses = l->lm_next;
940 prev->lm_next = l->lm_next;
946 ld->ld_errno = LDAP_SUCCESS;
949 if ( ber_sockbuf_ctrl( sb, LBER_SB_OPT_DATA_READY, NULL ) ) {
952 return( -2 ); /* continue looking */
957 build_result_ber( LDAP *ld, BerElement **bp, LDAPRequest *lr )
965 ber = ldap_alloc_ber_with_options( ld );
968 ld->ld_errno = LDAP_NO_MEMORY;
972 if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
973 lr->lr_res_msgtype, lr->lr_res_errno,
974 lr->lr_res_matched ? lr->lr_res_matched : "",
975 lr->lr_res_error ? lr->lr_res_error : "" ) == -1 )
977 ld->ld_errno = LDAP_ENCODING_ERROR;
979 return( LBER_ERROR );
984 if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) {
985 ld->ld_errno = LDAP_DECODING_ERROR;
987 return( LBER_ERROR );
990 if ( ber_get_enum( ber, &along ) == LBER_ERROR ) {
991 ld->ld_errno = LDAP_DECODING_ERROR;
993 return( LBER_ERROR );
996 tag = ber_peek_tag( ber, &len );
998 if ( tag == LBER_ERROR ) {
999 ld->ld_errno = LDAP_DECODING_ERROR;
1001 return( LBER_ERROR );
1010 merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
1013 * Merge error information in "lr" with "parentr" error code and string.
1015 if ( lr->lr_res_errno == LDAP_PARTIAL_RESULTS ) {
1016 parentr->lr_res_errno = lr->lr_res_errno;
1017 if ( lr->lr_res_error != NULL ) {
1018 (void)ldap_append_referral( ld, &parentr->lr_res_error,
1021 } else if ( lr->lr_res_errno != LDAP_SUCCESS &&
1022 parentr->lr_res_errno == LDAP_SUCCESS )
1024 parentr->lr_res_errno = lr->lr_res_errno;
1025 if ( parentr->lr_res_error != NULL ) {
1026 LDAP_FREE( parentr->lr_res_error );
1028 parentr->lr_res_error = lr->lr_res_error;
1029 lr->lr_res_error = NULL;
1030 if ( LDAP_NAME_ERROR( lr->lr_res_errno ) ) {
1031 if ( parentr->lr_res_matched != NULL ) {
1032 LDAP_FREE( parentr->lr_res_matched );
1034 parentr->lr_res_matched = lr->lr_res_matched;
1035 lr->lr_res_matched = NULL;
1039 Debug( LDAP_DEBUG_TRACE, "merged parent (id %d) error info: ",
1040 parentr->lr_msgid, 0, 0 );
1041 Debug( LDAP_DEBUG_TRACE, "result errno %d, error <%s>, matched <%s>\n",
1042 parentr->lr_res_errno, parentr->lr_res_error ?
1043 parentr->lr_res_error : "", parentr->lr_res_matched ?
1044 parentr->lr_res_matched : "" );
1050 ldap_msgtype( LDAPMessage *lm )
1052 assert( lm != NULL );
1053 return ( lm != NULL ) ? (int)lm->lm_msgtype : -1;
1058 ldap_msgid( LDAPMessage *lm )
1060 assert( lm != NULL );
1062 return ( lm != NULL ) ? lm->lm_msgid : -1;
1066 char * ldap_int_msgtype2str( ber_tag_t tag )
1069 case LDAP_RES_ADD: return "add";
1070 case LDAP_RES_BIND: return "bind";
1071 case LDAP_RES_COMPARE: return "compare";
1072 case LDAP_RES_DELETE: return "delete";
1073 case LDAP_RES_EXTENDED: return "extended-result";
1074 case LDAP_RES_INTERMEDIATE: return "intermediate";
1075 case LDAP_RES_MODIFY: return "modify";
1076 case LDAP_RES_RENAME: return "rename";
1077 case LDAP_RES_SEARCH_ENTRY: return "search-entry";
1078 case LDAP_RES_SEARCH_REFERENCE: return "search-reference";
1079 case LDAP_RES_SEARCH_RESULT: return "search-result";
1085 ldap_msgfree( LDAPMessage *lm )
1090 Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
1092 for ( ; lm != NULL; lm = next ) {
1093 next = lm->lm_chain;
1094 type = lm->lm_msgtype;
1095 ber_free( lm->lm_ber, 1 );
1096 LDAP_FREE( (char *) lm );
1103 * ldap_msgdelete - delete a message. It returns:
1104 * 0 if the entire message was deleted
1105 * -1 if the message was not found, or only part of it was found
1108 ldap_msgdelete( LDAP *ld, int msgid )
1110 LDAPMessage *lm, *prev;
1113 assert( ld != NULL );
1115 Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete\n", 0, 0, 0 );
1118 #ifdef LDAP_R_COMPILE
1119 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
1121 for ( lm = ld->ld_responses; lm != NULL; lm = lm->lm_next ) {
1122 if ( lm->lm_msgid == msgid )
1131 ld->ld_responses = lm->lm_next;
1133 prev->lm_next = lm->lm_next;
1135 #ifdef LDAP_R_COMPILE
1136 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
1138 if ( lm && ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY )
1146 * return 1 if message msgid is waiting to be abandoned, 0 otherwise
1149 ldap_abandoned( LDAP *ld, ber_int_t msgid )
1153 if ( ld->ld_abandoned == NULL )
1156 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
1157 if ( ld->ld_abandoned[i] == msgid )
1165 ldap_mark_abandoned( LDAP *ld, ber_int_t msgid )
1169 if ( ld->ld_abandoned == NULL )
1172 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
1173 if ( ld->ld_abandoned[i] == msgid )
1176 if ( ld->ld_abandoned[i] == -1 )
1179 for ( ; ld->ld_abandoned[i] != -1; i++ ) {
1180 ld->ld_abandoned[i] = ld->ld_abandoned[i + 1];