3 * Copyright 1998-2002 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
33 * LDAPResult ::= SEQUENCE {
34 * resultCode ENUMERATED { ... },
36 * errorMessage LDAPString,
37 * referral Referral OPTIONAL
39 * Referral ::= SEQUENCE OF LDAPURL (one or more)
40 * LDAPURL ::= LDAPString (limited to URL chars)
47 #include <ac/stdlib.h>
50 #include <ac/socket.h>
51 #include <ac/string.h>
53 #include <ac/unistd.h>
58 static int ldap_abandoned LDAP_P(( LDAP *ld, ber_int_t msgid ));
59 static int ldap_mark_abandoned LDAP_P(( LDAP *ld, ber_int_t msgid ));
60 static int wait4msg LDAP_P(( LDAP *ld, ber_int_t msgid, int all, struct timeval *timeout,
61 LDAPMessage **result ));
62 static ber_tag_t try_read1msg LDAP_P(( LDAP *ld, ber_int_t msgid,
63 int all, Sockbuf *sb, LDAPConn *lc, LDAPMessage **result ));
64 static ber_tag_t build_result_ber LDAP_P(( LDAP *ld, BerElement **bp, LDAPRequest *lr ));
65 static void merge_error_info LDAP_P(( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr ));
66 static LDAPMessage * chkResponseList LDAP_P(( LDAP *ld, int msgid, int all));
70 * ldap_result - wait for an ldap result response to a message from the
71 * ldap server. If msgid is LDAP_RES_ANY (-1), any message will be
72 * accepted. If msgid is LDAP_RES_UNSOLICITED (0), any unsolicited
73 * message is accepted. Otherwise ldap_result will wait for a response
74 * with msgid. If all is LDAP_MSG_ONE (0) the first message with id
75 * msgid will be accepted, otherwise, ldap_result will wait for all
76 * responses with id msgid and then return a pointer to the entire list
77 * of messages. In general, this is only useful for search responses,
78 * which can be of three message types (zero or more entries, zero or
79 * search references, followed by an ldap result). An extension to
80 * LDAPv3 allows partial extended responses to be returned in response
81 * to any request. The type of the first message received is returned.
82 * When waiting, any messages that have been abandoned are discarded.
85 * ldap_result( s, msgid, all, timeout, result )
92 struct timeval *timeout,
93 LDAPMessage **result )
98 assert( result != NULL );
101 LDAP_LOG (( "result", LDAP_LEVEL_ARGS, "ldap_result msgid %d\n", msgid ));
103 Debug( LDAP_DEBUG_TRACE, "ldap_result msgid %d\n", msgid, 0, 0 );
106 lm = chkResponseList(ld, msgid, all);
109 return( wait4msg( ld, msgid, all, timeout, result ) );
113 ld->ld_errno = LDAP_SUCCESS;
114 return( lm->lm_msgtype );
123 LDAPMessage *lm, *lastlm, *nextlm;
125 * Look through the list of responses we have received on
126 * this association and see if the response we're interested in
127 * is there. If it is, return it. If not, call wait4msg() to
128 * wait until it arrives or timeout occurs.
132 LDAP_LOG (( "result", LDAP_LEVEL_ARGS,
133 "ldap_chkResponseList for msgid=%d, all=%d\n", msgid, all ));
135 Debug( LDAP_DEBUG_TRACE,
136 "ldap_chkResponseList for msgid=%d, all=%d\n",
140 for ( lm = ld->ld_responses; lm != NULL; lm = nextlm ) {
141 nextlm = lm->lm_next;
143 if ( ldap_abandoned( ld, lm->lm_msgid ) ) {
145 LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1,
146 "ldap_chkResponseList msg abandoned, msgid %d\n", msgid ));
148 Debug( LDAP_DEBUG_TRACE,
149 "ldap_chkResponseList msg abandoned, msgid %d\n",
152 ldap_mark_abandoned( ld, lm->lm_msgid );
154 if ( lastlm == NULL ) {
155 /* Remove first entry in list */
156 ld->ld_responses = lm->lm_next;
158 lastlm->lm_next = nextlm;
166 if ( msgid == LDAP_RES_ANY || lm->lm_msgid == msgid ) {
169 if ( all == LDAP_MSG_ONE || msgid == LDAP_RES_UNSOLICITED ) {
173 for ( tmp = lm; tmp != NULL; tmp = tmp->lm_chain ) {
174 if ( tmp->lm_msgtype != LDAP_RES_SEARCH_ENTRY
175 && tmp->lm_msgtype != LDAP_RES_SEARCH_REFERENCE
176 && tmp->lm_msgtype != LDAP_RES_EXTENDED_PARTIAL )
192 /* Found an entry, remove it from the list */
193 if ( lastlm == NULL ) {
194 ld->ld_responses = (all == LDAP_MSG_ONE && lm->lm_chain != NULL
195 ? lm->lm_chain : lm->lm_next);
197 lastlm->lm_next = (all == LDAP_MSG_ONE && lm->lm_chain != NULL
198 ? lm->lm_chain : lm->lm_next);
200 if ( all == LDAP_MSG_ONE && lm->lm_chain != NULL ) {
201 lm->lm_chain->lm_next = lm->lm_next;
210 LDAP_LOG (( "result", LDAP_LEVEL_RESULTS,
211 "ldap_chkResponseList returns NULL\n" ));
213 Debug( LDAP_DEBUG_TRACE,
214 "ldap_chkResponseList returns NULL\n", 0, 0, 0);
218 LDAP_LOG (( "result", LDAP_LEVEL_RESULTS,
219 "ldap_chkResponseList returns msgid %d, type 0x02lu\n",
220 lm->lm_msgid, (unsigned long) lm->lm_msgtype ));
222 Debug( LDAP_DEBUG_TRACE,
223 "ldap_chkResponseList returns msgid %d, type 0x%02lu\n",
224 lm->lm_msgid, (unsigned long) lm->lm_msgtype, 0);
236 struct timeval *timeout,
237 LDAPMessage **result )
240 struct timeval tv, *tvp;
241 time_t start_time = 0;
243 LDAPConn *lc, *nextlc;
245 assert( ld != NULL );
246 assert( result != NULL );
249 if ( timeout == NULL ) {
251 LDAP_LOG (( "result", LDAP_LEVEL_ARGS,
252 "wait4msg (infinite timeout), msgid %d\n", msgid ));
254 Debug( LDAP_DEBUG_TRACE, "wait4msg (infinite timeout), msgid %d\n",
259 LDAP_LOG (( "result", LDAP_LEVEL_ARGS,
260 "wait4msg (timeout %ld sec, %ld usec), msgid %d\n",
261 (long) timeout->tv_sec, (long) timeout->tv_usec, msgid ));
263 Debug( LDAP_DEBUG_TRACE, "wait4msg (timeout %ld sec, %ld usec), msgid %d\n",
264 (long) timeout->tv_sec, (long) timeout->tv_usec, msgid );
267 #endif /* LDAP_DEBUG */
269 if ( timeout == NULL ) {
274 start_time = time( NULL );
281 LDAP_LOG (( "result", LDAP_LEVEL_ARGS,
282 "wait4msg continue, msgid %d, all %d\n", msgid, all ));
284 Debug( LDAP_DEBUG_TRACE, "wait4msg continue, msgid %d, all %d\n",
287 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
288 ldap_dump_connection( ld, ld->ld_conns, 1 );
289 ldap_dump_requests_and_responses( ld );
291 #endif /* LDAP_DEBUG */
293 if( (*result = chkResponseList(ld, msgid, all)) != NULL ) {
294 rc = (*result)->lm_msgtype;
297 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
298 if ( ber_sockbuf_ctrl( lc->lconn_sb,
299 LBER_SB_OPT_DATA_READY, NULL ) ) {
300 rc = try_read1msg( ld, msgid, all, lc->lconn_sb,
307 rc = ldap_int_select( ld, tvp );
313 LDAP_LOG (( "result", LDAP_LEVEL_ARGS,
314 "wait4msg: ldap_int_select returned -1: errno %d\n",
317 Debug( LDAP_DEBUG_TRACE,
318 "ldap_int_select returned -1: errno %d\n",
324 if ( rc == 0 || ( rc == -1 && (
325 !LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART)
326 || errno != EINTR )))
328 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
334 rc = -2; /* select interrupted: loop */
337 for ( lc = ld->ld_conns; rc == -2 && lc != NULL;
339 nextlc = lc->lconn_next;
340 if ( lc->lconn_status ==
341 LDAP_CONNST_CONNECTED &&
342 ldap_is_read_ready( ld,
344 rc = try_read1msg( ld, msgid, all,
345 lc->lconn_sb, lc, result );
352 if ( rc == -2 && tvp != NULL ) {
353 tmp_time = time( NULL );
354 if (( tv.tv_sec -= ( tmp_time - start_time )) <= 0 ) {
355 rc = 0; /* timed out */
356 ld->ld_errno = LDAP_TIMEOUT;
361 LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1,
362 "wait4msg: %ld secs to go\n", (long) tv.tv_sec ));
364 Debug( LDAP_DEBUG_TRACE, "wait4msg: %ld secs to go\n",
365 (long) tv.tv_sec, 0, 0 );
367 start_time = tmp_time;
382 LDAPMessage **result )
385 LDAPMessage *new, *l, *prev, *tmp;
390 LDAPRequest *lr, *tmplr;
392 int rc, refer_cnt, hadref, simple_request;
395 * v3ref = flag for V3 referral / search reference
396 * 0 = not a ref, 1 = sucessfully chased ref, -1 = pass ref to application
400 assert( ld != NULL );
401 assert( lc != NULL );
404 LDAP_LOG (( "result", LDAP_LEVEL_ARGS, "read1msg: msgid %d, all %d\n",
407 Debug( LDAP_DEBUG_TRACE, "read1msg: msgid %d, all %d\n", msgid, all, 0 );
410 if ( lc->lconn_ber == NULL ) {
411 lc->lconn_ber = ldap_alloc_ber_with_options(ld);
413 if( lc->lconn_ber == NULL ) {
419 assert( LBER_VALID (ber) );
421 /* get the next message */
423 #ifdef LDAP_CONNECTIONLESS
424 if ( LDAP_IS_UDP(ld) ) {
425 struct sockaddr from;
426 ber_int_sb_read(sb, &from, sizeof(struct sockaddr));
429 if ( (tag = ber_get_next( sb, &len, ber ))
430 != LDAP_TAG_MESSAGE ) {
431 if ( tag == LBER_DEFAULT) {
434 LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1,
435 "read1msg: ber_get_next failed\n" ));
437 Debug( LDAP_DEBUG_CONNS,
438 "ber_get_next failed.\n", 0, 0, 0 );
442 if (errno==EWOULDBLOCK) return -2;
445 if (errno == EAGAIN) return -2;
447 ld->ld_errno = LDAP_SERVER_DOWN;
450 ld->ld_errno = LDAP_LOCAL_ERROR;
455 * We read a complete message.
456 * The connection should no longer need this ber.
458 lc->lconn_ber = NULL;
461 if ( ber_get_int( ber, &id ) == LBER_ERROR ) {
463 ld->ld_errno = LDAP_DECODING_ERROR;
467 /* if it's been abandoned, toss it */
468 if ( ldap_abandoned( ld, id ) ) {
471 LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1,
472 "read1msg: abandoned\n" ));
474 Debug( LDAP_DEBUG_ANY, "abandoned\n", 0, 0, 0);
476 return( -2 ); /* continue looking */
479 if (( lr = ldap_find_request_by_msgid( ld, id )) == NULL ) {
481 LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1,
482 "read1msg: no request for response with msgid %ld (tossing)\n",
485 Debug( LDAP_DEBUG_ANY,
486 "no request for response with msgid %ld (tossing)\n",
490 return( -2 ); /* continue looking */
492 #ifdef LDAP_CONNECTIONLESS
493 if (LDAP_IS_UDP(ld) && ld->ld_options.ldo_version == LDAP_VERSION2) {
495 ber_scanf(ber, "m{", &blank);
498 /* the message type */
499 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
500 ld->ld_errno = LDAP_DECODING_ERROR;
506 LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1,
507 "read1msg: ldap_read: message type %s msgid %ld, original id %ld\n",
508 ldap_int_msgtype2str( tag ),
509 (long) lr->lr_msgid, (long) lr->lr_origid ));
511 Debug( LDAP_DEBUG_TRACE,
512 "ldap_read: message type %s msgid %ld, original id %ld\n",
513 ldap_int_msgtype2str( tag ),
514 (long) lr->lr_msgid, (long) lr->lr_origid );
519 hadref = simple_request = 0;
520 rc = -2; /* default is to keep looking (no response found) */
521 lr->lr_res_msgtype = tag;
524 * This code figures out if we are going to chase a
525 * referral / search reference, or pass it back to the application
527 v3ref = 0; /* Assume not a V3 search reference or referral */
528 if( (tag != LDAP_RES_SEARCH_ENTRY) && (ld->ld_version > LDAP_VERSION2) ) {
529 BerElement tmpber = *ber; /* struct copy */
532 if( tag == LDAP_RES_SEARCH_REFERENCE) {
533 /* This is a V3 search reference */
534 /* Assume we do not chase the reference, 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 arrary 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 ) { /* sucessfully chased reference */
547 /* If haven't got end search, set chasing referrals */
548 if( lr->lr_status != LDAP_REQST_COMPLETED) {
549 lr->lr_status = LDAP_REQST_CHASINGREFS;
551 LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1,
552 "read1msg: search ref chased,"
553 "mark request chasing refs, id = %d\n",
556 Debug( LDAP_DEBUG_TRACE,
557 "read1msg: search ref chased, mark request chasing refs, id = %d\n",
561 v3ref = 1; /* We sucessfully chased the reference */
566 /* Check for V3 referral */
568 if ( ber_scanf( &tmpber, "{iaa",/*}*/ &lderr,
569 &lr->lr_res_matched, &lr->lr_res_error )
571 /* Check if V3 referral */
572 if( ber_peek_tag( &tmpber, &len) == LDAP_TAG_REFERRAL ) {
573 /* We have a V3 referral, assume we cannot chase it */
575 if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS)
576 || (lr->lr_parent != NULL) )
578 v3ref = -1; /* Assume referral not chased and return it to app */
579 /* Get the referral list */
580 if( ber_scanf( &tmpber, "{v}", &refs) == LBER_ERROR) {
581 rc = LDAP_DECODING_ERROR;
582 lr->lr_status = LDAP_REQST_COMPLETED;
584 LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1,
585 "read1msg: referral decode error,"
586 "mark request completed, id = %d\n",
589 Debug( LDAP_DEBUG_TRACE,
590 "read1msg: referral decode error, mark request completed, id = %d\n",
594 /* Chase the referral
595 * Note: refs arrary is freed by ldap_chase_v3referrals
597 refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
598 0, &lr->lr_res_error, &hadref );
599 lr->lr_status = LDAP_REQST_COMPLETED;
601 LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1,
602 "read1msg: referral chased,"
603 "mark request completed, id = %d\n",
606 Debug( LDAP_DEBUG_TRACE,
607 "read1msg: referral chased, mark request completed, id = %d\n",
611 v3ref = 1; /* Referral successfully chased */
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 > -1) ) {
634 /* For a v3 search referral/reference, only come here if already chased it */
635 if ( ld->ld_version >= LDAP_VERSION2 &&
636 ( lr->lr_parent != NULL ||
637 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ) )
639 tmpber = *ber; /* struct copy */
641 /* V3 search reference or V3 referral
642 * sucessfully chased. If this message
643 * is a search result, then it has no more
644 * outstanding referrals.
646 if ( tag == LDAP_RES_SEARCH_RESULT )
648 } else if ( ber_scanf( &tmpber, "{iaa}", &lderr,
649 &lr->lr_res_matched, &lr->lr_res_error )
651 if ( lderr != LDAP_SUCCESS ) {
652 /* referrals are in error string */
653 refer_cnt = ldap_chase_referrals( ld, lr,
654 &lr->lr_res_error, -1, &hadref );
655 lr->lr_status = LDAP_REQST_COMPLETED;
657 LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1,
658 "read1msg: V2 referral chased,"
659 "mark request completed, id = %d\n",
662 Debug( LDAP_DEBUG_TRACE,
663 "read1msg: V2 referral chased, mark request completed, id = %d\n", lr->lr_msgid, 0, 0);
667 /* save errno, message, and matched string */
668 if ( !hadref || lr->lr_res_error == NULL ) {
669 lr->lr_res_errno = ( lderr ==
670 LDAP_PARTIAL_RESULTS ) ? LDAP_SUCCESS
672 } else if ( ld->ld_errno != LDAP_SUCCESS ) {
673 lr->lr_res_errno = ld->ld_errno;
675 lr->lr_res_errno = LDAP_PARTIAL_RESULTS;
678 LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1,
679 "read1msg: new result: res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
680 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
681 lr->lr_res_matched ? lr->lr_res_matched : "" ));
683 Debug( LDAP_DEBUG_TRACE,
684 "new result: res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
685 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
686 lr->lr_res_matched ? lr->lr_res_matched : "" );
692 LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1,
693 "read1msg: %d new referrals\n", refer_cnt ));
695 Debug( LDAP_DEBUG_TRACE,
696 "read1msg: %d new referrals\n", refer_cnt, 0, 0 );
699 if ( refer_cnt != 0 ) { /* chasing referrals */
702 if ( refer_cnt < 0 ) {
703 return( -1 ); /* fatal error */
705 lr->lr_res_errno = LDAP_SUCCESS; /* sucessfully chased referral */
707 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
708 /* request without any referrals */
709 simple_request = ( hadref ? 0 : 1 );
711 /* request with referrals or child request */
716 lr->lr_status = LDAP_REQST_COMPLETED; /* declare this request done */
718 LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1,
719 "read1msg: mark request completed, id = %d\n", lr->lr_msgid ));
721 Debug( LDAP_DEBUG_TRACE,
722 "read1msg: mark request completed, id = %d\n", lr->lr_msgid, 0, 0);
724 while ( lr->lr_parent != NULL ) {
725 merge_error_info( ld, lr->lr_parent, lr );
728 if ( --lr->lr_outrefcnt > 0 ) {
729 break; /* not completely done yet */
733 /* Check if all requests are finished, lr is now parent */
735 if (tmplr->lr_status == LDAP_REQST_COMPLETED) {
736 for(tmplr=lr->lr_child; tmplr != NULL; tmplr=tmplr->lr_refnext) {
737 if( tmplr->lr_status != LDAP_REQST_COMPLETED) {
743 /* This is the parent request if the request has referrals */
744 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL && tmplr == NULL ) {
746 tag = lr->lr_res_msgtype;
748 LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1,
749 "read1msg: request %ld done\n", (long) id ));
750 LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1,
751 "read1msg: res_errno: %d,res_error: <%s>, res_matched: <%s>\n",
752 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
753 lr->lr_res_matched ? lr->lr_res_matched : "" ));
755 Debug( LDAP_DEBUG_ANY, "request %ld done\n",
757 Debug( LDAP_DEBUG_TRACE,
758 "res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
759 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
760 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 ldap_free_request( ld, lr );
775 ldap_free_connection( ld, lc, 0, 1 );
784 /* make a new ldap message */
785 if ( (new = (LDAPMessage *) LDAP_CALLOC( 1, sizeof(LDAPMessage) ))
787 ld->ld_errno = LDAP_NO_MEMORY;
790 new->lm_msgid = (int)id;
791 new->lm_msgtype = tag;
795 if ( ld->ld_cache != NULL ) {
796 ldap_add_result_to_cache( ld, new );
798 #endif /* LDAP_NOCACHE */
800 /* is this the one we're looking for? */
801 if ( msgid == LDAP_RES_ANY || id == msgid ) {
802 if ( all == LDAP_MSG_ONE
803 || (new->lm_msgtype != LDAP_RES_SEARCH_RESULT
804 && new->lm_msgtype != LDAP_RES_SEARCH_ENTRY
805 && new->lm_msgtype != LDAP_RES_SEARCH_REFERENCE) ) {
807 ld->ld_errno = LDAP_SUCCESS;
809 } else if ( new->lm_msgtype == LDAP_RES_SEARCH_RESULT) {
810 foundit = 1; /* return the chain later */
815 * if not, we must add it to the list of responses. if
816 * the msgid is already there, it must be part of an existing
821 for ( l = ld->ld_responses; l != NULL; l = l->lm_next ) {
822 if ( l->lm_msgid == new->lm_msgid )
827 /* not part of an existing search response */
831 ld->ld_errno = LDAP_SUCCESS;
835 new->lm_next = ld->ld_responses;
836 ld->ld_responses = new;
837 return( -2 ); /* continue looking */
841 LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1,
842 "read1msg: adding response id %ld type %ld\n",
843 (long) new->lm_msgid, (long) new->lm_msgtype ));
845 Debug( LDAP_DEBUG_TRACE, "adding response id %ld type %ld:\n",
846 (long) new->lm_msgid, (long) new->lm_msgtype, 0 );
849 /* part of a search response - add to end of list of entries */
850 for ( tmp = l; (tmp->lm_chain != NULL) &&
851 ((tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY) ||
852 (tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_REFERENCE) ||
853 (tmp->lm_chain->lm_msgtype == LDAP_RES_EXTENDED_PARTIAL ));
854 tmp = tmp->lm_chain )
858 /* return the whole chain if that's what we were looking for */
861 ld->ld_responses = l->lm_next;
863 prev->lm_next = l->lm_next;
865 ld->ld_errno = LDAP_SUCCESS;
866 #ifdef LDAP_WORLD_P16
868 * XXX questionable fix; see text for [P16] on
869 * http://www.critical-angle.com/ldapworld/patch/
871 * inclusion of this patch causes searchs to hang on
874 return( l->lm_msgtype );
875 #else /* LDAP_WORLD_P16 */
877 #endif /* !LDAP_WORLD_P16 */
880 return( -2 ); /* continue looking */
885 build_result_ber( LDAP *ld, BerElement **bp, LDAPRequest *lr )
893 ber = ldap_alloc_ber_with_options( ld );
896 ld->ld_errno = LDAP_NO_MEMORY;
900 if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
901 lr->lr_res_msgtype, lr->lr_res_errno,
902 lr->lr_res_matched ? lr->lr_res_matched : "",
903 lr->lr_res_error ? lr->lr_res_error : "" ) == -1 ) {
905 ld->ld_errno = LDAP_ENCODING_ERROR;
907 return( LBER_ERROR );
912 if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) {
913 ld->ld_errno = LDAP_DECODING_ERROR;
915 return( LBER_ERROR );
918 if ( ber_get_int( ber, &along ) == LBER_ERROR ) {
919 ld->ld_errno = LDAP_DECODING_ERROR;
921 return( LBER_ERROR );
924 tag = ber_peek_tag( ber, &len );
926 if ( tag == LBER_ERROR ) {
927 ld->ld_errno = LDAP_DECODING_ERROR;
929 return( LBER_ERROR );
938 merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
941 * Merge error information in "lr" with "parentr" error code and string.
943 if ( lr->lr_res_errno == LDAP_PARTIAL_RESULTS ) {
944 parentr->lr_res_errno = lr->lr_res_errno;
945 if ( lr->lr_res_error != NULL ) {
946 (void)ldap_append_referral( ld, &parentr->lr_res_error,
949 } else if ( lr->lr_res_errno != LDAP_SUCCESS &&
950 parentr->lr_res_errno == LDAP_SUCCESS ) {
951 parentr->lr_res_errno = lr->lr_res_errno;
952 if ( parentr->lr_res_error != NULL ) {
953 LDAP_FREE( parentr->lr_res_error );
955 parentr->lr_res_error = lr->lr_res_error;
956 lr->lr_res_error = NULL;
957 if ( LDAP_NAME_ERROR( lr->lr_res_errno )) {
958 if ( parentr->lr_res_matched != NULL ) {
959 LDAP_FREE( parentr->lr_res_matched );
961 parentr->lr_res_matched = lr->lr_res_matched;
962 lr->lr_res_matched = NULL;
967 LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1,
968 "read1msg: merged parent (id %d) error info: result errno %d, "
969 "error <%s>, matched <%s>\n", parentr->lr_msgid,
970 parentr->lr_res_errno, parentr->lr_res_error ?
971 parentr->lr_res_error : "", parentr->lr_res_matched ?
972 parentr->lr_res_matched : "" ));
974 Debug( LDAP_DEBUG_TRACE, "merged parent (id %d) error info: ",
975 parentr->lr_msgid, 0, 0 );
976 Debug( LDAP_DEBUG_TRACE, "result errno %d, error <%s>, matched <%s>\n",
977 parentr->lr_res_errno, parentr->lr_res_error ?
978 parentr->lr_res_error : "", parentr->lr_res_matched ?
979 parentr->lr_res_matched : "" );
986 ldap_msgtype( LDAPMessage *lm )
988 assert( lm != NULL );
989 return ( lm != NULL ) ? lm->lm_msgtype : -1;
994 ldap_msgid( LDAPMessage *lm )
996 assert( lm != NULL );
998 return ( lm != NULL ) ? lm->lm_msgid : -1;
1002 char * ldap_int_msgtype2str( ber_tag_t tag )
1005 case LDAP_RES_ADD: return "add";
1006 case LDAP_RES_BIND: return "bind";
1007 case LDAP_RES_COMPARE: return "compare";
1008 case LDAP_RES_DELETE: return "delete";
1009 case LDAP_RES_EXTENDED: return "extended-result";
1010 case LDAP_RES_EXTENDED_PARTIAL: return "extended-partial";
1011 case LDAP_RES_MODIFY: return "modify";
1012 case LDAP_RES_RENAME: return "rename";
1013 case LDAP_RES_SEARCH_ENTRY: return "search-entry";
1014 case LDAP_RES_SEARCH_REFERENCE: return "search-reference";
1015 case LDAP_RES_SEARCH_RESULT: return "search-result";
1021 ldap_msgfree( LDAPMessage *lm )
1027 LDAP_LOG (( "result", LDAP_LEVEL_ENTRY, "ldap_msgfree\n" ));
1029 Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
1032 for ( ; lm != NULL; lm = next ) {
1033 next = lm->lm_chain;
1034 type = lm->lm_msgtype;
1035 ber_free( lm->lm_ber, 1 );
1036 LDAP_FREE( (char *) lm );
1043 * ldap_msgdelete - delete a message. It returns:
1044 * 0 if the entire message was deleted
1045 * -1 if the message was not found, or only part of it was found
1048 ldap_msgdelete( LDAP *ld, int msgid )
1050 LDAPMessage *lm, *prev;
1052 assert( ld != NULL );
1055 LDAP_LOG (( "result", LDAP_LEVEL_ENTRY, "ldap_msgdelete\n" ));
1057 Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete\n", 0, 0, 0 );
1061 for ( lm = ld->ld_responses; lm != NULL; lm = lm->lm_next ) {
1062 if ( lm->lm_msgid == msgid )
1071 ld->ld_responses = lm->lm_next;
1073 prev->lm_next = lm->lm_next;
1075 if ( ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY )
1083 * return 1 if message msgid is waiting to be abandoned, 0 otherwise
1086 ldap_abandoned( LDAP *ld, ber_int_t msgid )
1090 if ( ld->ld_abandoned == NULL )
1093 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
1094 if ( ld->ld_abandoned[i] == msgid )
1102 ldap_mark_abandoned( LDAP *ld, ber_int_t msgid )
1106 if ( ld->ld_abandoned == NULL )
1109 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
1110 if ( ld->ld_abandoned[i] == msgid )
1113 if ( ld->ld_abandoned[i] == -1 )
1116 for ( ; ld->ld_abandoned[i] != -1; i++ ) {
1117 ld->ld_abandoned[i] = ld->ld_abandoned[i + 1];