3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1990 Regents of the University of Michigan.
11 * This notice applies to changes, created by or for Novell, Inc.,
12 * to preexisting works for which notices appear elsewhere in this file.
14 * Copyright (C) 1999, 2000 Novell, Inc. All Rights Reserved.
16 * THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND TREATIES.
17 * USE, MODIFICATION, AND REDISTRIBUTION OF THIS WORK IS SUBJECT TO VERSION
18 * 2.0.1 OF THE OPENLDAP PUBLIC LICENSE, A COPY OF WHICH IS AVAILABLE AT
19 * HTTP://WWW.OPENLDAP.ORG/LICENSE.HTML OR IN THE FILE "LICENSE" IN THE
20 * TOP-LEVEL DIRECTORY OF THE DISTRIBUTION. ANY USE OR EXPLOITATION OF THIS
21 * WORK OTHER THAN AS AUTHORIZED IN VERSION 2.0.1 OF THE OPENLDAP PUBLIC
22 * LICENSE, OR OTHER PRIOR WRITTEN CONSENT FROM NOVELL, COULD SUBJECT THE
23 * PERPETRATOR TO CRIMINAL AND CIVIL LIABILITY.
25 * Modification to OpenLDAP source by Novell, Inc.
26 * April 2000 sfs Add code to process V3 referrals and search results
28 * result.c - wait for an ldap result
30 /* Note: A verbatim copy of version 2.0.1 of the OpenLDAP Public License
31 * can be found in the file "build/LICENSE-2.0.1" in this distribution
32 * of OpenLDAP Software.
37 * LDAPResult ::= SEQUENCE {
38 * resultCode ENUMERATED { ... },
40 * errorMessage LDAPString,
41 * referral Referral OPTIONAL
43 * Referral ::= SEQUENCE OF LDAPURL (one or more)
44 * LDAPURL ::= LDAPString (limited to URL chars)
51 #include <ac/stdlib.h>
54 #include <ac/socket.h>
55 #include <ac/string.h>
57 #include <ac/unistd.h>
62 static int ldap_abandoned LDAP_P(( LDAP *ld, ber_int_t msgid ));
63 static int ldap_mark_abandoned LDAP_P(( LDAP *ld, ber_int_t msgid ));
64 static int wait4msg LDAP_P(( LDAP *ld, ber_int_t msgid, int all, struct timeval *timeout,
65 LDAPMessage **result ));
66 static ber_tag_t try_read1msg LDAP_P(( LDAP *ld, ber_int_t msgid,
67 int all, Sockbuf *sb, LDAPConn *lc, LDAPMessage **result ));
68 static ber_tag_t build_result_ber LDAP_P(( LDAP *ld, BerElement **bp, LDAPRequest *lr ));
69 static void merge_error_info LDAP_P(( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr ));
70 static LDAPMessage * chkResponseList LDAP_P(( LDAP *ld, int msgid, int all));
74 * ldap_result - wait for an ldap result response to a message from the
75 * ldap server. If msgid is LDAP_RES_ANY (-1), any message will be
76 * accepted. If msgid is LDAP_RES_UNSOLICITED (0), any unsolicited
77 * message is accepted. Otherwise ldap_result will wait for a response
78 * with msgid. If all is LDAP_MSG_ONE (0) the first message with id
79 * msgid will be accepted, otherwise, ldap_result will wait for all
80 * responses with id msgid and then return a pointer to the entire list
81 * of messages. In general, this is only useful for search responses,
82 * which can be of three message types (zero or more entries, zero or
83 * search references, followed by an ldap result). An extension to
84 * LDAPv3 allows partial extended responses to be returned in response
85 * to any request. The type of the first message received is returned.
86 * When waiting, any messages that have been abandoned are discarded.
89 * ldap_result( s, msgid, all, timeout, result )
96 struct timeval *timeout,
97 LDAPMessage **result )
102 assert( ld != NULL );
103 assert( result != NULL );
106 LDAP_LOG ( OPERATION, ARGS, "ldap_result msgid %d\n", msgid, 0, 0 );
108 Debug( LDAP_DEBUG_TRACE, "ldap_result msgid %d\n", msgid, 0, 0 );
111 #ifdef LDAP_R_COMPILE
112 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
114 lm = chkResponseList(ld, msgid, all);
117 rc = wait4msg( ld, msgid, all, timeout, result );
120 ld->ld_errno = LDAP_SUCCESS;
123 #ifdef LDAP_R_COMPILE
124 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
135 LDAPMessage *lm, *lastlm, *nextlm;
137 * Look through the list of responses we have received on
138 * this association and see if the response we're interested in
139 * is there. If it is, return it. If not, call wait4msg() to
140 * wait until it arrives or timeout occurs.
144 LDAP_LOG ( OPERATION, ARGS, "ldap_chkResponseList for msgid=%d, all=%d\n",
147 Debug( LDAP_DEBUG_TRACE,
148 "ldap_chkResponseList for msgid=%d, all=%d\n",
152 for ( lm = ld->ld_responses; lm != NULL; lm = nextlm ) {
153 nextlm = lm->lm_next;
155 if ( ldap_abandoned( ld, lm->lm_msgid ) ) {
157 LDAP_LOG ( OPERATION, DETAIL1,
158 "ldap_chkResponseList msg abandoned, msgid %d\n", msgid, 0, 0 );
160 Debug( LDAP_DEBUG_TRACE,
161 "ldap_chkResponseList msg abandoned, msgid %d\n",
164 ldap_mark_abandoned( ld, lm->lm_msgid );
166 if ( lastlm == NULL ) {
167 /* Remove first entry in list */
168 ld->ld_responses = lm->lm_next;
170 lastlm->lm_next = nextlm;
178 if ( msgid == LDAP_RES_ANY || lm->lm_msgid == msgid ) {
181 if ( all == LDAP_MSG_ONE || msgid == LDAP_RES_UNSOLICITED ) {
185 for ( tmp = lm; tmp != NULL; tmp = tmp->lm_chain ) {
186 if ( tmp->lm_msgtype != LDAP_RES_SEARCH_ENTRY
187 && tmp->lm_msgtype != LDAP_RES_SEARCH_REFERENCE
188 && tmp->lm_msgtype != LDAP_RES_EXTENDED_PARTIAL )
204 /* Found an entry, remove it from the list */
205 if ( lastlm == NULL ) {
206 ld->ld_responses = (all == LDAP_MSG_ONE && lm->lm_chain != NULL
207 ? lm->lm_chain : lm->lm_next);
209 lastlm->lm_next = (all == LDAP_MSG_ONE && lm->lm_chain != NULL
210 ? lm->lm_chain : lm->lm_next);
212 if ( all == LDAP_MSG_ONE && lm->lm_chain != NULL ) {
213 lm->lm_chain->lm_next = lm->lm_next;
222 LDAP_LOG ( OPERATION, RESULTS, "ldap_chkResponseList returns NULL\n",
225 Debug( LDAP_DEBUG_TRACE,
226 "ldap_chkResponseList returns NULL\n", 0, 0, 0);
230 LDAP_LOG ( OPERATION, RESULTS,
231 "ldap_chkResponseList returns msgid %d, type 0x%02lu\n",
232 lm->lm_msgid, (unsigned long) lm->lm_msgtype, 0 );
234 Debug( LDAP_DEBUG_TRACE,
235 "ldap_chkResponseList returns msgid %d, type 0x%02lu\n",
236 lm->lm_msgid, (unsigned long) lm->lm_msgtype, 0);
248 struct timeval *timeout,
249 LDAPMessage **result )
252 struct timeval tv, *tvp;
253 time_t start_time = 0;
255 LDAPConn *lc, *nextlc;
257 assert( ld != NULL );
258 assert( result != NULL );
261 if ( timeout == NULL ) {
263 LDAP_LOG ( OPERATION, ARGS,
264 "wait4msg (infinite timeout), msgid %d\n", msgid, 0, 0 );
266 Debug( LDAP_DEBUG_TRACE, "wait4msg (infinite timeout), msgid %d\n",
271 LDAP_LOG ( OPERATION, ARGS,
272 "wait4msg (timeout %ld sec, %ld usec), msgid %d\n",
273 (long) timeout->tv_sec, (long) timeout->tv_usec, msgid );
275 Debug( LDAP_DEBUG_TRACE, "wait4msg (timeout %ld sec, %ld usec), msgid %d\n",
276 (long) timeout->tv_sec, (long) timeout->tv_usec, msgid );
279 #endif /* LDAP_DEBUG */
281 if ( timeout == NULL ) {
286 start_time = time( NULL );
293 LDAP_LOG ( OPERATION, ARGS,
294 "wait4msg continue, msgid %d, all %d\n", msgid, all, 0 );
296 Debug( LDAP_DEBUG_TRACE, "wait4msg continue, msgid %d, all %d\n",
299 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
300 ldap_dump_connection( ld, ld->ld_conns, 1 );
301 ldap_dump_requests_and_responses( ld );
303 #endif /* LDAP_DEBUG */
305 if( (*result = chkResponseList(ld, msgid, all)) != NULL ) {
306 rc = (*result)->lm_msgtype;
309 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
310 if ( ber_sockbuf_ctrl( lc->lconn_sb,
311 LBER_SB_OPT_DATA_READY, NULL ) ) {
312 rc = try_read1msg( ld, msgid, all, lc->lconn_sb,
319 rc = ldap_int_select( ld, tvp );
323 LDAP_LOG ( OPERATION, ARGS,
324 "wait4msg: ldap_int_select returned -1: errno %d\n",
327 Debug( LDAP_DEBUG_TRACE,
328 "ldap_int_select returned -1: errno %d\n",
334 if ( rc == 0 || ( rc == -1 && (
335 !LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART)
336 || errno != EINTR )))
338 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
344 rc = -2; /* select interrupted: loop */
347 #ifdef LDAP_R_COMPILE
348 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
350 if ( ld->ld_requests &&
351 ld->ld_requests->lr_status == LDAP_REQST_WRITING &&
352 ldap_is_write_ready( ld,
353 ld->ld_requests->lr_conn->lconn_sb ) ) {
354 ldap_int_flush_request( ld, ld->ld_requests );
356 #ifdef LDAP_R_COMPILE
357 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
359 for ( lc = ld->ld_conns; rc == -2 && lc != NULL;
361 nextlc = lc->lconn_next;
362 if ( lc->lconn_status ==
363 LDAP_CONNST_CONNECTED &&
364 ldap_is_read_ready( ld,
366 rc = try_read1msg( ld, msgid, all,
367 lc->lconn_sb, lc, result );
374 if ( rc == -2 && tvp != NULL ) {
375 tmp_time = time( NULL );
376 if (( tv.tv_sec -= ( tmp_time - start_time )) <= 0 ) {
377 rc = 0; /* timed out */
378 ld->ld_errno = LDAP_TIMEOUT;
383 LDAP_LOG ( OPERATION, DETAIL1,
384 "wait4msg: %ld secs to go\n", (long) tv.tv_sec, 0, 0 );
386 Debug( LDAP_DEBUG_TRACE, "wait4msg: %ld secs to go\n",
387 (long) tv.tv_sec, 0, 0 );
389 start_time = tmp_time;
404 LDAPMessage **result )
407 LDAPMessage *new, *l, *prev, *tmp;
412 LDAPRequest *lr, *tmplr;
414 int rc, refer_cnt, hadref, simple_request;
416 #ifdef LDAP_CONNECTIONLESS
417 int firstmsg = 1, moremsgs = 0, isv2 = 0;
420 * v3ref = flag for V3 referral / search reference
421 * 0 = not a ref, 1 = sucessfully chased ref, -1 = pass ref to application
425 assert( ld != NULL );
426 assert( lc != NULL );
429 LDAP_LOG ( OPERATION, ARGS, "read1msg: msgid %d, all %d\n", msgid, all, 0 );
431 Debug( LDAP_DEBUG_TRACE, "read1msg: msgid %d, all %d\n", msgid, all, 0 );
435 if ( lc->lconn_ber == NULL ) {
436 lc->lconn_ber = ldap_alloc_ber_with_options(ld);
438 if( lc->lconn_ber == NULL ) {
444 assert( LBER_VALID (ber) );
446 /* get the next message */
448 #ifdef LDAP_CONNECTIONLESS
449 if ( LDAP_IS_UDP(ld) ) {
450 struct sockaddr from;
451 ber_int_sb_read(sb, &from, sizeof(struct sockaddr));
452 if (ld->ld_options.ldo_version == LDAP_VERSION2) isv2=1;
456 tag = ber_get_next( sb, &len, ber );
457 if ( tag == LDAP_TAG_MESSAGE ) {
459 * We read a complete message.
460 * The connection should no longer need this ber.
462 lc->lconn_ber = NULL;
464 if ( tag != LDAP_TAG_MESSAGE ) {
465 if ( tag == LBER_DEFAULT) {
468 LDAP_LOG ( OPERATION, DETAIL1,
469 "read1msg: ber_get_next failed\n", 0, 0, 0 );
471 Debug( LDAP_DEBUG_CONNS,
472 "ber_get_next failed.\n", 0, 0, 0 );
476 if (errno==EWOULDBLOCK) return -2;
479 if (errno == EAGAIN) return -2;
481 ld->ld_errno = LDAP_SERVER_DOWN;
484 ld->ld_errno = LDAP_LOCAL_ERROR;
489 if ( ber_get_int( ber, &id ) == LBER_ERROR ) {
491 ld->ld_errno = LDAP_DECODING_ERROR;
495 /* if it's been abandoned, toss it */
496 if ( ldap_abandoned( ld, id ) ) {
498 LDAP_LOG ( OPERATION, DETAIL1, "read1msg: abandoned\n", 0, 0, 0 );
500 Debug( LDAP_DEBUG_ANY, "abandoned\n", 0, 0, 0);
504 if ( ber_sockbuf_ctrl( sb, LBER_SB_OPT_DATA_READY, NULL ) ) {
507 return( -2 ); /* continue looking */
510 if (( lr = ldap_find_request_by_msgid( ld, id )) == NULL ) {
512 LDAP_LOG ( OPERATION, DETAIL1,
513 "read1msg: no request for response with msgid %ld (tossing)\n",
516 Debug( LDAP_DEBUG_ANY,
517 "no request for response with msgid %ld (tossing)\n",
522 #ifdef LDAP_CONNECTIONLESS
523 if (LDAP_IS_UDP(ld) && isv2) {
524 ber_scanf(ber, "x{");
528 /* the message type */
529 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
530 ld->ld_errno = LDAP_DECODING_ERROR;
536 LDAP_LOG ( OPERATION, DETAIL1,
537 "read1msg: ldap_read: message type %s msgid %ld, original id %ld\n",
538 ldap_int_msgtype2str( tag ),
539 (long) lr->lr_msgid, (long) lr->lr_origid );
541 Debug( LDAP_DEBUG_TRACE,
542 "ldap_read: message type %s msgid %ld, original id %ld\n",
543 ldap_int_msgtype2str( tag ),
544 (long) lr->lr_msgid, (long) lr->lr_origid );
549 hadref = simple_request = 0;
550 rc = -2; /* default is to keep looking (no response found) */
551 lr->lr_res_msgtype = tag;
554 * This code figures out if we are going to chase a
555 * referral / search reference, or pass it back to the application
557 v3ref = 0; /* Assume not a V3 search reference or referral */
558 if( (tag != LDAP_RES_SEARCH_ENTRY) && (ld->ld_version > LDAP_VERSION2) ) {
559 BerElement tmpber = *ber; /* struct copy */
562 if( tag == LDAP_RES_SEARCH_REFERENCE) {
563 /* This is a V3 search reference */
564 /* Assume we do not chase the reference, but pass it to application */
566 if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ||
567 (lr->lr_parent != NULL) )
569 /* Get the referral list */
570 if ( ber_scanf( &tmpber, "{v}", &refs ) == LBER_ERROR ) {
571 rc = LDAP_DECODING_ERROR;
573 /* Note: refs arrary is freed by ldap_chase_v3referrals */
574 refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
575 1, &lr->lr_res_error, &hadref );
576 if ( refer_cnt > 0 ) { /* sucessfully chased reference */
577 /* If haven't got end search, set chasing referrals */
578 if( lr->lr_status != LDAP_REQST_COMPLETED) {
579 lr->lr_status = LDAP_REQST_CHASINGREFS;
581 LDAP_LOG ( OPERATION, DETAIL1,
582 "read1msg: search ref chased,"
583 "mark request chasing refs, id = %d\n",
584 lr->lr_msgid, 0, 0 );
586 Debug( LDAP_DEBUG_TRACE,
587 "read1msg: search ref chased, mark request chasing refs, id = %d\n",
591 v3ref = 1; /* We sucessfully chased the reference */
596 /* Check for V3 referral */
598 if ( ber_scanf( &tmpber, "{iaa",/*}*/ &lderr,
599 &lr->lr_res_matched, &lr->lr_res_error )
601 /* Check if V3 referral */
602 if( ber_peek_tag( &tmpber, &len) == LDAP_TAG_REFERRAL ) {
603 /* We have a V3 referral, assume we cannot chase it */
605 if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS)
606 || (lr->lr_parent != NULL) )
608 v3ref = -1; /* 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;
614 LDAP_LOG ( OPERATION, DETAIL1,
615 "read1msg: referral decode error,"
616 "mark request completed, id = %d\n",
617 lr->lr_msgid, 0, 0 );
619 Debug( LDAP_DEBUG_TRACE,
620 "read1msg: referral decode error, mark request completed, id = %d\n",
624 /* Chase the referral
625 * Note: refs arrary is freed by ldap_chase_v3referrals
627 refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
628 0, &lr->lr_res_error, &hadref );
629 lr->lr_status = LDAP_REQST_COMPLETED;
631 LDAP_LOG ( OPERATION, DETAIL1,
632 "read1msg: referral chased,"
633 "mark request completed, id = %d\n",
634 lr->lr_msgid, 0, 0 );
636 Debug( LDAP_DEBUG_TRACE,
637 "read1msg: referral chased, mark request completed, id = %d\n",
641 v3ref = 1; /* Referral successfully chased */
647 if( lr->lr_res_matched != NULL ) {
648 LDAP_FREE( lr->lr_res_matched );
649 lr->lr_res_matched = NULL;
651 if( lr->lr_res_error != NULL ) {
652 LDAP_FREE( lr->lr_res_error );
653 lr->lr_res_error = NULL;
659 /* All results that just return a status, i.e. don't return data
660 * go through the following code. This code also chases V2 referrals
661 * and checks if all referrals have been chased.
663 if ( (tag != LDAP_RES_SEARCH_ENTRY) && (v3ref > -1)
664 #ifdef LDAP_RES_INTERMEDIATE_RESP
665 && (tag != LDAP_RES_INTERMEDIATE_RESP )
668 /* For a v3 search referral/reference, only come here if already chased it */
669 if ( ld->ld_version >= LDAP_VERSION2 &&
670 ( lr->lr_parent != NULL ||
671 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ) )
673 tmpber = *ber; /* struct copy */
675 /* V3 search reference or V3 referral
676 * sucessfully chased. If this message
677 * is a search result, then it has no more
678 * outstanding referrals.
680 if ( tag == LDAP_RES_SEARCH_RESULT )
682 } else if ( ber_scanf( &tmpber, "{iaa}", &lderr,
683 &lr->lr_res_matched, &lr->lr_res_error )
685 if ( lderr != LDAP_SUCCESS ) {
686 /* referrals are in error string */
687 refer_cnt = ldap_chase_referrals( ld, lr,
688 &lr->lr_res_error, -1, &hadref );
689 lr->lr_status = LDAP_REQST_COMPLETED;
691 LDAP_LOG ( OPERATION, DETAIL1,
692 "read1msg: V2 referral chased,"
693 "mark request completed, id = %d\n",
694 lr->lr_msgid, 0, 0 );
696 Debug( LDAP_DEBUG_TRACE,
697 "read1msg: V2 referral chased, mark request completed, id = %d\n", lr->lr_msgid, 0, 0);
701 /* save errno, message, and matched string */
702 if ( !hadref || lr->lr_res_error == NULL ) {
703 lr->lr_res_errno = ( lderr ==
704 LDAP_PARTIAL_RESULTS ) ? LDAP_SUCCESS
706 } else if ( ld->ld_errno != LDAP_SUCCESS ) {
707 lr->lr_res_errno = ld->ld_errno;
709 lr->lr_res_errno = LDAP_PARTIAL_RESULTS;
712 LDAP_LOG ( OPERATION, DETAIL1,
713 "read1msg: new result: res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
714 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
715 lr->lr_res_matched ? lr->lr_res_matched : "" );
717 Debug( LDAP_DEBUG_TRACE,
718 "new result: res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
719 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
720 lr->lr_res_matched ? lr->lr_res_matched : "" );
726 LDAP_LOG ( OPERATION, DETAIL1, "read1msg: %d new referrals\n",
729 Debug( LDAP_DEBUG_TRACE,
730 "read1msg: %d new referrals\n", refer_cnt, 0, 0 );
733 if ( refer_cnt != 0 ) { /* chasing referrals */
736 if ( refer_cnt < 0 ) {
737 return( -1 ); /* fatal error */
739 lr->lr_res_errno = LDAP_SUCCESS; /* sucessfully chased referral */
741 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
742 /* request without any referrals */
743 simple_request = ( hadref ? 0 : 1 );
745 /* request with referrals or child request */
750 lr->lr_status = LDAP_REQST_COMPLETED; /* declare this request done */
752 LDAP_LOG ( OPERATION, DETAIL1,
753 "read1msg: mark request completed, id = %d\n",
754 lr->lr_msgid, 0, 0 );
756 Debug( LDAP_DEBUG_TRACE,
757 "read1msg: mark request completed, id = %d\n", lr->lr_msgid, 0, 0);
759 while ( lr->lr_parent != NULL ) {
760 merge_error_info( ld, lr->lr_parent, lr );
763 if ( --lr->lr_outrefcnt > 0 ) {
764 break; /* not completely done yet */
768 /* Check if all requests are finished, lr is now parent */
770 if (tmplr->lr_status == LDAP_REQST_COMPLETED) {
771 for(tmplr=lr->lr_child; tmplr != NULL; tmplr=tmplr->lr_refnext) {
772 if( tmplr->lr_status != LDAP_REQST_COMPLETED) {
778 /* This is the parent request if the request has referrals */
779 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL && tmplr == NULL ) {
781 tag = lr->lr_res_msgtype;
783 LDAP_LOG ( OPERATION, DETAIL1,
784 "read1msg: request %ld done\n", (long) id, 0, 0 );
785 LDAP_LOG ( OPERATION, DETAIL1,
786 "read1msg: res_errno: %d,res_error: <%s>, res_matched: <%s>\n",
787 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
788 lr->lr_res_matched ? lr->lr_res_matched : "" );
790 Debug( LDAP_DEBUG_ANY, "request %ld done\n",
792 Debug( LDAP_DEBUG_TRACE,
793 "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 if ( !simple_request ) {
800 if ( build_result_ber( ld, &ber, lr )
802 rc = -1; /* fatal error */
806 ldap_free_request( ld, lr );
810 ldap_free_connection( ld, lc, 0, 1 );
819 /* make a new ldap message */
820 if ( (new = (LDAPMessage *) LDAP_CALLOC( 1, sizeof(LDAPMessage) ))
822 ld->ld_errno = LDAP_NO_MEMORY;
825 new->lm_msgid = (int)id;
826 new->lm_msgtype = tag;
829 #ifdef LDAP_CONNECTIONLESS
830 /* CLDAP replies all fit in a single datagram. In LDAPv2 RFC1798
831 * the responses are all a sequence wrapped in one message. In
832 * LDAPv3 each response is in its own message. The datagram must
833 * end with a SearchResult. We can't just parse each response in
834 * separate calls to try_read1msg because the header info is only
835 * present at the beginning of the datagram, not at the beginning
836 * of each response. So parse all the responses at once and queue
837 * them up, then pull off the first response to return to the
838 * caller when all parsing is complete.
840 if ( LDAP_IS_UDP(ld) ) {
841 /* If not a result, look for more */
842 if ( tag != LDAP_RES_SEARCH_RESULT ) {
846 /* LDAPv2: dup the current ber, skip past the current
847 * response, and see if there are any more after it.
849 ber = ber_dup( ber );
850 ber_scanf( ber, "x" );
851 if (ber_peek_tag(ber, &len) != LBER_DEFAULT) {
852 /* There's more - dup the ber buffer so they can all be
853 * individually freed by ldap_msgfree.
856 ber_get_option(ber, LBER_OPT_BER_REMAINING_BYTES, &len);
857 bv.bv_val = LDAP_MALLOC(len);
860 ber_read(ber, bv.bv_val, len);
862 ber_init2(ber, &bv, ld->ld_lberoptions );
866 /* LDAPv3: Just allocate a new ber. Since this is a buffered
867 * datagram, if the sockbuf is readable we still have data
870 ber = ldap_alloc_ber_with_options(ld);
871 if (ber_sockbuf_ctrl(sb, LBER_SB_OPT_DATA_READY, NULL)) ok=1;
873 /* set up response chain */
876 new->lm_next = ld->ld_responses;
877 ld->ld_responses = new;
882 /* "ok" means there's more to parse */
884 if (isv2) goto nextresp2;
887 /* got to end of datagram without a SearchResult. Free
888 * our dup'd ber, but leave any buffer alone. For v2 case,
889 * the previous response is still using this buffer. For v3,
890 * the new ber has no buffer to free yet.
895 } else if ( moremsgs ) {
896 /* got search result, and we had multiple responses in 1 datagram.
897 * stick the result onto the end of the chain, and then pull the
898 * first response off the head of the chain.
901 *result = chkResponseList( ld, msgid, all );
902 ld->ld_errno = LDAP_SUCCESS;
903 return( (*result)->lm_msgtype );
908 /* is this the one we're looking for? */
909 if ( msgid == LDAP_RES_ANY || id == msgid ) {
910 if ( all == LDAP_MSG_ONE
911 || (new->lm_msgtype != LDAP_RES_SEARCH_RESULT
912 && new->lm_msgtype != LDAP_RES_SEARCH_ENTRY
913 && new->lm_msgtype != LDAP_RES_SEARCH_REFERENCE) ) {
915 ld->ld_errno = LDAP_SUCCESS;
917 } else if ( new->lm_msgtype == LDAP_RES_SEARCH_RESULT) {
918 foundit = 1; /* return the chain later */
923 * if not, we must add it to the list of responses. if
924 * the msgid is already there, it must be part of an existing
929 for ( l = ld->ld_responses; l != NULL; l = l->lm_next ) {
930 if ( l->lm_msgid == new->lm_msgid )
935 /* not part of an existing search response */
942 new->lm_next = ld->ld_responses;
943 ld->ld_responses = new;
948 LDAP_LOG ( OPERATION, DETAIL1,
949 "read1msg: adding response id %ld type %ld\n",
950 (long) new->lm_msgid, (long) new->lm_msgtype, 0 );
952 Debug( LDAP_DEBUG_TRACE, "adding response id %ld type %ld:\n",
953 (long) new->lm_msgid, (long) new->lm_msgtype, 0 );
956 /* part of a search response - add to end of list of entries */
957 for ( tmp = l; (tmp->lm_chain != NULL) &&
958 ((tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY) ||
959 (tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_REFERENCE) ||
960 (tmp->lm_chain->lm_msgtype == LDAP_RES_EXTENDED_PARTIAL ));
961 tmp = tmp->lm_chain )
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 ( ber_sockbuf_ctrl( sb, LBER_SB_OPT_DATA_READY, NULL ) ) {
982 return( -2 ); /* 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_int( 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 ) {
1053 parentr->lr_res_errno = lr->lr_res_errno;
1054 if ( parentr->lr_res_error != NULL ) {
1055 LDAP_FREE( parentr->lr_res_error );
1057 parentr->lr_res_error = lr->lr_res_error;
1058 lr->lr_res_error = NULL;
1059 if ( LDAP_NAME_ERROR( lr->lr_res_errno )) {
1060 if ( parentr->lr_res_matched != NULL ) {
1061 LDAP_FREE( parentr->lr_res_matched );
1063 parentr->lr_res_matched = lr->lr_res_matched;
1064 lr->lr_res_matched = NULL;
1069 LDAP_LOG( OPERATION, DETAIL1, "merged parent (id %d) error info: ",
1070 parentr->lr_msgid, 0, 0 );
1071 LDAP_LOG( OPERATION, DETAIL1, "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 : "" );
1076 Debug( LDAP_DEBUG_TRACE, "merged parent (id %d) error info: ",
1077 parentr->lr_msgid, 0, 0 );
1078 Debug( LDAP_DEBUG_TRACE, "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 : "" );
1088 ldap_msgtype( LDAPMessage *lm )
1090 assert( lm != NULL );
1091 return ( lm != NULL ) ? lm->lm_msgtype : -1;
1096 ldap_msgid( LDAPMessage *lm )
1098 assert( lm != NULL );
1100 return ( lm != NULL ) ? lm->lm_msgid : -1;
1104 char * ldap_int_msgtype2str( ber_tag_t tag )
1107 case LDAP_RES_ADD: return "add";
1108 case LDAP_RES_BIND: return "bind";
1109 case LDAP_RES_COMPARE: return "compare";
1110 case LDAP_RES_DELETE: return "delete";
1111 case LDAP_RES_EXTENDED: return "extended-result";
1112 case LDAP_RES_EXTENDED_PARTIAL: return "extended-partial";
1113 case LDAP_RES_MODIFY: return "modify";
1114 case LDAP_RES_RENAME: return "rename";
1115 case LDAP_RES_SEARCH_ENTRY: return "search-entry";
1116 case LDAP_RES_SEARCH_REFERENCE: return "search-reference";
1117 case LDAP_RES_SEARCH_RESULT: return "search-result";
1123 ldap_msgfree( LDAPMessage *lm )
1129 LDAP_LOG ( OPERATION, ENTRY, "ldap_msgfree\n", 0, 0, 0 );
1131 Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
1134 for ( ; lm != NULL; lm = next ) {
1135 next = lm->lm_chain;
1136 type = lm->lm_msgtype;
1137 ber_free( lm->lm_ber, 1 );
1138 LDAP_FREE( (char *) lm );
1145 * ldap_msgdelete - delete a message. It returns:
1146 * 0 if the entire message was deleted
1147 * -1 if the message was not found, or only part of it was found
1150 ldap_msgdelete( LDAP *ld, int msgid )
1152 LDAPMessage *lm, *prev;
1155 assert( ld != NULL );
1158 LDAP_LOG ( OPERATION, ENTRY, "ldap_msgdelete\n", 0, 0, 0 );
1160 Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete\n", 0, 0, 0 );
1164 #ifdef LDAP_R_COMPILE
1165 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
1167 for ( lm = ld->ld_responses; lm != NULL; lm = lm->lm_next ) {
1168 if ( lm->lm_msgid == msgid )
1177 ld->ld_responses = lm->lm_next;
1179 prev->lm_next = lm->lm_next;
1181 #ifdef LDAP_R_COMPILE
1182 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
1184 if ( lm && ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY )
1192 * return 1 if message msgid is waiting to be abandoned, 0 otherwise
1195 ldap_abandoned( LDAP *ld, ber_int_t msgid )
1199 if ( ld->ld_abandoned == NULL )
1202 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
1203 if ( ld->ld_abandoned[i] == msgid )
1211 ldap_mark_abandoned( LDAP *ld, ber_int_t msgid )
1215 if ( ld->ld_abandoned == NULL )
1218 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
1219 if ( ld->ld_abandoned[i] == msgid )
1222 if ( ld->ld_abandoned[i] == -1 )
1225 for ( ; ld->ld_abandoned[i] != -1; i++ ) {
1226 ld->ld_abandoned[i] = ld->ld_abandoned[i + 1];