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
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 )
101 assert( ld != NULL );
102 assert( result != NULL );
105 LDAP_LOG ( OPERATION, ARGS, "ldap_result msgid %d\n", msgid, 0, 0 );
107 Debug( LDAP_DEBUG_TRACE, "ldap_result msgid %d\n", msgid, 0, 0 );
110 lm = chkResponseList(ld, msgid, all);
113 return( wait4msg( ld, msgid, all, timeout, result ) );
117 ld->ld_errno = LDAP_SUCCESS;
118 return( lm->lm_msgtype );
127 LDAPMessage *lm, *lastlm, *nextlm;
129 * Look through the list of responses we have received on
130 * this association and see if the response we're interested in
131 * is there. If it is, return it. If not, call wait4msg() to
132 * wait until it arrives or timeout occurs.
136 LDAP_LOG ( OPERATION, ARGS, "ldap_chkResponseList for msgid=%d, all=%d\n",
139 Debug( LDAP_DEBUG_TRACE,
140 "ldap_chkResponseList for msgid=%d, all=%d\n",
144 for ( lm = ld->ld_responses; lm != NULL; lm = nextlm ) {
145 nextlm = lm->lm_next;
147 if ( ldap_abandoned( ld, lm->lm_msgid ) ) {
149 LDAP_LOG ( OPERATION, DETAIL1,
150 "ldap_chkResponseList msg abandoned, msgid %d\n", msgid, 0, 0 );
152 Debug( LDAP_DEBUG_TRACE,
153 "ldap_chkResponseList msg abandoned, msgid %d\n",
156 ldap_mark_abandoned( ld, lm->lm_msgid );
158 if ( lastlm == NULL ) {
159 /* Remove first entry in list */
160 ld->ld_responses = lm->lm_next;
162 lastlm->lm_next = nextlm;
170 if ( msgid == LDAP_RES_ANY || lm->lm_msgid == msgid ) {
173 if ( all == LDAP_MSG_ONE || msgid == LDAP_RES_UNSOLICITED ) {
177 for ( tmp = lm; tmp != NULL; tmp = tmp->lm_chain ) {
178 if ( tmp->lm_msgtype != LDAP_RES_SEARCH_ENTRY
179 && tmp->lm_msgtype != LDAP_RES_SEARCH_REFERENCE
180 && tmp->lm_msgtype != LDAP_RES_EXTENDED_PARTIAL )
196 /* Found an entry, remove it from the list */
197 if ( lastlm == NULL ) {
198 ld->ld_responses = (all == LDAP_MSG_ONE && lm->lm_chain != NULL
199 ? lm->lm_chain : lm->lm_next);
201 lastlm->lm_next = (all == LDAP_MSG_ONE && lm->lm_chain != NULL
202 ? lm->lm_chain : lm->lm_next);
204 if ( all == LDAP_MSG_ONE && lm->lm_chain != NULL ) {
205 lm->lm_chain->lm_next = lm->lm_next;
214 LDAP_LOG ( OPERATION, RESULTS, "ldap_chkResponseList returns NULL\n",
217 Debug( LDAP_DEBUG_TRACE,
218 "ldap_chkResponseList returns NULL\n", 0, 0, 0);
222 LDAP_LOG ( OPERATION, RESULTS,
223 "ldap_chkResponseList returns msgid %d, type 0x%02lu\n",
224 lm->lm_msgid, (unsigned long) lm->lm_msgtype, 0 );
226 Debug( LDAP_DEBUG_TRACE,
227 "ldap_chkResponseList returns msgid %d, type 0x%02lu\n",
228 lm->lm_msgid, (unsigned long) lm->lm_msgtype, 0);
240 struct timeval *timeout,
241 LDAPMessage **result )
244 struct timeval tv, *tvp;
245 time_t start_time = 0;
247 LDAPConn *lc, *nextlc;
249 assert( ld != NULL );
250 assert( result != NULL );
253 if ( timeout == NULL ) {
255 LDAP_LOG ( OPERATION, ARGS,
256 "wait4msg (infinite timeout), msgid %d\n", msgid, 0, 0 );
258 Debug( LDAP_DEBUG_TRACE, "wait4msg (infinite timeout), msgid %d\n",
263 LDAP_LOG ( OPERATION, ARGS,
264 "wait4msg (timeout %ld sec, %ld usec), msgid %d\n",
265 (long) timeout->tv_sec, (long) timeout->tv_usec, msgid );
267 Debug( LDAP_DEBUG_TRACE, "wait4msg (timeout %ld sec, %ld usec), msgid %d\n",
268 (long) timeout->tv_sec, (long) timeout->tv_usec, msgid );
271 #endif /* LDAP_DEBUG */
273 if ( timeout == NULL ) {
278 start_time = time( NULL );
285 LDAP_LOG ( OPERATION, ARGS,
286 "wait4msg continue, msgid %d, all %d\n", msgid, all, 0 );
288 Debug( LDAP_DEBUG_TRACE, "wait4msg continue, msgid %d, all %d\n",
291 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
292 ldap_dump_connection( ld, ld->ld_conns, 1 );
293 ldap_dump_requests_and_responses( ld );
295 #endif /* LDAP_DEBUG */
297 if( (*result = chkResponseList(ld, msgid, all)) != NULL ) {
298 rc = (*result)->lm_msgtype;
301 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
302 if ( ber_sockbuf_ctrl( lc->lconn_sb,
303 LBER_SB_OPT_DATA_READY, NULL ) ) {
304 rc = try_read1msg( ld, msgid, all, lc->lconn_sb,
311 rc = ldap_int_select( ld, tvp );
317 LDAP_LOG ( OPERATION, ARGS,
318 "wait4msg: ldap_int_select returned -1: errno %d\n",
321 Debug( LDAP_DEBUG_TRACE,
322 "ldap_int_select returned -1: errno %d\n",
328 if ( rc == 0 || ( rc == -1 && (
329 !LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART)
330 || errno != EINTR )))
332 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
338 rc = -2; /* select interrupted: loop */
341 if ( ld->ld_requests &&
342 ld->ld_requests->lr_status == LDAP_REQST_WRITING &&
343 ldap_is_write_ready( ld,
344 ld->ld_requests->lr_conn->lconn_sb ) ) {
345 ldap_int_flush_request( ld, ld->ld_requests );
347 for ( lc = ld->ld_conns; rc == -2 && lc != NULL;
349 nextlc = lc->lconn_next;
350 if ( lc->lconn_status ==
351 LDAP_CONNST_CONNECTED &&
352 ldap_is_read_ready( ld,
354 rc = try_read1msg( ld, msgid, all,
355 lc->lconn_sb, lc, result );
362 if ( rc == -2 && tvp != NULL ) {
363 tmp_time = time( NULL );
364 if (( tv.tv_sec -= ( tmp_time - start_time )) <= 0 ) {
365 rc = 0; /* timed out */
366 ld->ld_errno = LDAP_TIMEOUT;
371 LDAP_LOG ( OPERATION, DETAIL1,
372 "wait4msg: %ld secs to go\n", (long) tv.tv_sec, 0, 0 );
374 Debug( LDAP_DEBUG_TRACE, "wait4msg: %ld secs to go\n",
375 (long) tv.tv_sec, 0, 0 );
377 start_time = tmp_time;
392 LDAPMessage **result )
395 LDAPMessage *new, *l, *prev, *tmp;
400 LDAPRequest *lr, *tmplr;
402 int rc, refer_cnt, hadref, simple_request;
405 * v3ref = flag for V3 referral / search reference
406 * 0 = not a ref, 1 = sucessfully chased ref, -1 = pass ref to application
410 assert( ld != NULL );
411 assert( lc != NULL );
414 LDAP_LOG ( OPERATION, ARGS, "read1msg: msgid %d, all %d\n", msgid, all, 0 );
416 Debug( LDAP_DEBUG_TRACE, "read1msg: msgid %d, all %d\n", msgid, all, 0 );
420 if ( lc->lconn_ber == NULL ) {
421 lc->lconn_ber = ldap_alloc_ber_with_options(ld);
423 if( lc->lconn_ber == NULL ) {
429 assert( LBER_VALID (ber) );
432 /* get the next message */
434 #ifdef LDAP_CONNECTIONLESS
435 if ( LDAP_IS_UDP(ld) ) {
436 struct sockaddr from;
437 ber_int_sb_read(sb, &from, sizeof(struct sockaddr));
440 if ( (tag = ber_get_next( sb, &len, ber ))
441 != LDAP_TAG_MESSAGE ) {
442 if ( tag == LBER_DEFAULT) {
445 LDAP_LOG ( OPERATION, DETAIL1,
446 "read1msg: ber_get_next failed\n", 0, 0, 0 );
448 Debug( LDAP_DEBUG_CONNS,
449 "ber_get_next failed.\n", 0, 0, 0 );
453 if (errno==EWOULDBLOCK) return -2;
456 if (errno == EAGAIN) return -2;
458 ld->ld_errno = LDAP_SERVER_DOWN;
461 ld->ld_errno = LDAP_LOCAL_ERROR;
466 * We read a complete message.
467 * The connection should no longer need this ber.
469 lc->lconn_ber = NULL;
472 if ( ber_get_int( ber, &id ) == LBER_ERROR ) {
474 ld->ld_errno = LDAP_DECODING_ERROR;
478 /* if it's been abandoned, toss it */
479 if ( ldap_abandoned( ld, id ) ) {
481 LDAP_LOG ( OPERATION, DETAIL1, "read1msg: abandoned\n", 0, 0, 0 );
483 Debug( LDAP_DEBUG_ANY, "abandoned\n", 0, 0, 0);
486 if ( ber_sockbuf_ctrl( sb, LBER_SB_OPT_DATA_READY, NULL ) ) {
488 ber_init2( ber, NULL, ld->ld_lberoptions );
492 return( -2 ); /* continue looking */
495 if (( lr = ldap_find_request_by_msgid( ld, id )) == NULL ) {
497 LDAP_LOG ( OPERATION, DETAIL1,
498 "read1msg: no request for response with msgid %ld (tossing)\n",
501 Debug( LDAP_DEBUG_ANY,
502 "no request for response with msgid %ld (tossing)\n",
507 #ifdef LDAP_CONNECTIONLESS
508 if (LDAP_IS_UDP(ld) && ld->ld_options.ldo_version == LDAP_VERSION2) {
510 ber_scanf(ber, "m{", &blank);
513 /* the message type */
514 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
515 ld->ld_errno = LDAP_DECODING_ERROR;
521 LDAP_LOG ( OPERATION, DETAIL1,
522 "read1msg: ldap_read: message type %s msgid %ld, original id %ld\n",
523 ldap_int_msgtype2str( tag ),
524 (long) lr->lr_msgid, (long) lr->lr_origid );
526 Debug( LDAP_DEBUG_TRACE,
527 "ldap_read: message type %s msgid %ld, original id %ld\n",
528 ldap_int_msgtype2str( tag ),
529 (long) lr->lr_msgid, (long) lr->lr_origid );
534 hadref = simple_request = 0;
535 rc = -2; /* default is to keep looking (no response found) */
536 lr->lr_res_msgtype = tag;
539 * This code figures out if we are going to chase a
540 * referral / search reference, or pass it back to the application
542 v3ref = 0; /* Assume not a V3 search reference or referral */
543 if( (tag != LDAP_RES_SEARCH_ENTRY) && (ld->ld_version > LDAP_VERSION2) ) {
544 BerElement tmpber = *ber; /* struct copy */
547 if( tag == LDAP_RES_SEARCH_REFERENCE) {
548 /* This is a V3 search reference */
549 /* Assume we do not chase the reference, but pass it to application */
551 if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ||
552 (lr->lr_parent != NULL) )
554 /* Get the referral list */
555 if ( ber_scanf( &tmpber, "{v}", &refs ) == LBER_ERROR ) {
556 rc = LDAP_DECODING_ERROR;
558 /* Note: refs arrary is freed by ldap_chase_v3referrals */
559 refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
560 1, &lr->lr_res_error, &hadref );
561 if ( refer_cnt > 0 ) { /* 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;
566 LDAP_LOG ( OPERATION, DETAIL1,
567 "read1msg: search ref chased,"
568 "mark request chasing refs, id = %d\n",
569 lr->lr_msgid, 0, 0 );
571 Debug( LDAP_DEBUG_TRACE,
572 "read1msg: search ref chased, mark request chasing refs, id = %d\n",
576 v3ref = 1; /* We sucessfully chased the reference */
581 /* Check for V3 referral */
583 if ( ber_scanf( &tmpber, "{iaa",/*}*/ &lderr,
584 &lr->lr_res_matched, &lr->lr_res_error )
586 /* Check if V3 referral */
587 if( ber_peek_tag( &tmpber, &len) == LDAP_TAG_REFERRAL ) {
588 /* We have a V3 referral, assume we cannot chase it */
590 if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS)
591 || (lr->lr_parent != NULL) )
593 v3ref = -1; /* Assume referral not chased and return it to app */
594 /* Get the referral list */
595 if( ber_scanf( &tmpber, "{v}", &refs) == LBER_ERROR) {
596 rc = LDAP_DECODING_ERROR;
597 lr->lr_status = LDAP_REQST_COMPLETED;
599 LDAP_LOG ( OPERATION, DETAIL1,
600 "read1msg: referral decode error,"
601 "mark request completed, id = %d\n",
602 lr->lr_msgid, 0, 0 );
604 Debug( LDAP_DEBUG_TRACE,
605 "read1msg: referral decode error, mark request completed, id = %d\n",
609 /* Chase the referral
610 * Note: refs arrary is freed by ldap_chase_v3referrals
612 refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
613 0, &lr->lr_res_error, &hadref );
614 lr->lr_status = LDAP_REQST_COMPLETED;
616 LDAP_LOG ( OPERATION, DETAIL1,
617 "read1msg: referral chased,"
618 "mark request completed, id = %d\n",
619 lr->lr_msgid, 0, 0 );
621 Debug( LDAP_DEBUG_TRACE,
622 "read1msg: referral chased, mark request completed, id = %d\n",
626 v3ref = 1; /* Referral successfully chased */
632 if( lr->lr_res_matched != NULL ) {
633 LDAP_FREE( lr->lr_res_matched );
634 lr->lr_res_matched = NULL;
636 if( lr->lr_res_error != NULL ) {
637 LDAP_FREE( lr->lr_res_error );
638 lr->lr_res_error = NULL;
644 /* All results that just return a status, i.e. don't return data
645 * go through the following code. This code also chases V2 referrals
646 * and checks if all referrals have been chased.
648 if ( (tag != LDAP_RES_SEARCH_ENTRY) && (v3ref > -1) ) {
649 /* For a v3 search referral/reference, only come here if already chased it */
650 if ( ld->ld_version >= LDAP_VERSION2 &&
651 ( lr->lr_parent != NULL ||
652 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ) )
654 tmpber = *ber; /* struct copy */
656 /* V3 search reference or V3 referral
657 * sucessfully chased. If this message
658 * is a search result, then it has no more
659 * outstanding referrals.
661 if ( tag == LDAP_RES_SEARCH_RESULT )
663 } else if ( ber_scanf( &tmpber, "{iaa}", &lderr,
664 &lr->lr_res_matched, &lr->lr_res_error )
666 if ( lderr != LDAP_SUCCESS ) {
667 /* referrals are in error string */
668 refer_cnt = ldap_chase_referrals( ld, lr,
669 &lr->lr_res_error, -1, &hadref );
670 lr->lr_status = LDAP_REQST_COMPLETED;
672 LDAP_LOG ( OPERATION, DETAIL1,
673 "read1msg: V2 referral chased,"
674 "mark request completed, id = %d\n",
675 lr->lr_msgid, 0, 0 );
677 Debug( LDAP_DEBUG_TRACE,
678 "read1msg: V2 referral chased, mark request completed, id = %d\n", lr->lr_msgid, 0, 0);
682 /* save errno, message, and matched string */
683 if ( !hadref || lr->lr_res_error == NULL ) {
684 lr->lr_res_errno = ( lderr ==
685 LDAP_PARTIAL_RESULTS ) ? LDAP_SUCCESS
687 } else if ( ld->ld_errno != LDAP_SUCCESS ) {
688 lr->lr_res_errno = ld->ld_errno;
690 lr->lr_res_errno = LDAP_PARTIAL_RESULTS;
693 LDAP_LOG ( OPERATION, DETAIL1,
694 "read1msg: new result: res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
695 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
696 lr->lr_res_matched ? lr->lr_res_matched : "" );
698 Debug( LDAP_DEBUG_TRACE,
699 "new result: res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
700 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
701 lr->lr_res_matched ? lr->lr_res_matched : "" );
707 LDAP_LOG ( OPERATION, DETAIL1, "read1msg: %d new referrals\n",
710 Debug( LDAP_DEBUG_TRACE,
711 "read1msg: %d new referrals\n", refer_cnt, 0, 0 );
714 if ( refer_cnt != 0 ) { /* chasing referrals */
717 if ( refer_cnt < 0 ) {
718 return( -1 ); /* fatal error */
720 lr->lr_res_errno = LDAP_SUCCESS; /* sucessfully chased referral */
722 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
723 /* request without any referrals */
724 simple_request = ( hadref ? 0 : 1 );
726 /* request with referrals or child request */
731 lr->lr_status = LDAP_REQST_COMPLETED; /* declare this request done */
733 LDAP_LOG ( OPERATION, DETAIL1,
734 "read1msg: mark request completed, id = %d\n",
735 lr->lr_msgid, 0, 0 );
737 Debug( LDAP_DEBUG_TRACE,
738 "read1msg: mark request completed, id = %d\n", lr->lr_msgid, 0, 0);
740 while ( lr->lr_parent != NULL ) {
741 merge_error_info( ld, lr->lr_parent, lr );
744 if ( --lr->lr_outrefcnt > 0 ) {
745 break; /* not completely done yet */
749 /* Check if all requests are finished, lr is now parent */
751 if (tmplr->lr_status == LDAP_REQST_COMPLETED) {
752 for(tmplr=lr->lr_child; tmplr != NULL; tmplr=tmplr->lr_refnext) {
753 if( tmplr->lr_status != LDAP_REQST_COMPLETED) {
759 /* This is the parent request if the request has referrals */
760 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL && tmplr == NULL ) {
762 tag = lr->lr_res_msgtype;
764 LDAP_LOG ( OPERATION, DETAIL1,
765 "read1msg: request %ld done\n", (long) id, 0, 0 );
766 LDAP_LOG ( OPERATION, DETAIL1,
767 "read1msg: res_errno: %d,res_error: <%s>, res_matched: <%s>\n",
768 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
769 lr->lr_res_matched ? lr->lr_res_matched : "" );
771 Debug( LDAP_DEBUG_ANY, "request %ld done\n",
773 Debug( LDAP_DEBUG_TRACE,
774 "res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
775 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
776 lr->lr_res_matched ? lr->lr_res_matched : "" );
778 if ( !simple_request ) {
781 if ( build_result_ber( ld, &ber, lr )
783 rc = -1; /* fatal error */
787 ldap_free_request( ld, lr );
791 ldap_free_connection( ld, lc, 0, 1 );
800 /* make a new ldap message */
801 if ( (new = (LDAPMessage *) LDAP_CALLOC( 1, sizeof(LDAPMessage) ))
803 ld->ld_errno = LDAP_NO_MEMORY;
806 new->lm_msgid = (int)id;
807 new->lm_msgtype = tag;
811 if ( ld->ld_cache != NULL ) {
812 ldap_add_result_to_cache( ld, new );
814 #endif /* LDAP_NOCACHE */
816 /* is this the one we're looking for? */
817 if ( msgid == LDAP_RES_ANY || id == msgid ) {
818 if ( all == LDAP_MSG_ONE
819 || (new->lm_msgtype != LDAP_RES_SEARCH_RESULT
820 && new->lm_msgtype != LDAP_RES_SEARCH_ENTRY
821 && new->lm_msgtype != LDAP_RES_SEARCH_REFERENCE) ) {
823 ld->ld_errno = LDAP_SUCCESS;
825 } else if ( new->lm_msgtype == LDAP_RES_SEARCH_RESULT) {
826 foundit = 1; /* return the chain later */
831 * if not, we must add it to the list of responses. if
832 * the msgid is already there, it must be part of an existing
837 for ( l = ld->ld_responses; l != NULL; l = l->lm_next ) {
838 if ( l->lm_msgid == new->lm_msgid )
843 /* not part of an existing search response */
847 ld->ld_errno = LDAP_SUCCESS;
851 new->lm_next = ld->ld_responses;
852 ld->ld_responses = new;
857 LDAP_LOG ( OPERATION, DETAIL1,
858 "read1msg: adding response id %ld type %ld\n",
859 (long) new->lm_msgid, (long) new->lm_msgtype, 0 );
861 Debug( LDAP_DEBUG_TRACE, "adding response id %ld type %ld:\n",
862 (long) new->lm_msgid, (long) new->lm_msgtype, 0 );
865 /* part of a search response - add to end of list of entries */
866 for ( tmp = l; (tmp->lm_chain != NULL) &&
867 ((tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY) ||
868 (tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_REFERENCE) ||
869 (tmp->lm_chain->lm_msgtype == LDAP_RES_EXTENDED_PARTIAL ));
870 tmp = tmp->lm_chain )
874 /* return the whole chain if that's what we were looking for */
877 ld->ld_responses = l->lm_next;
879 prev->lm_next = l->lm_next;
881 ld->ld_errno = LDAP_SUCCESS;
882 #ifdef LDAP_WORLD_P16
884 * XXX questionable fix; see text for [P16] on
885 * http://www.critical-angle.com/ldapworld/patch/
887 * inclusion of this patch causes searchs to hang on
890 return( l->lm_msgtype );
891 #else /* LDAP_WORLD_P16 */
893 #endif /* !LDAP_WORLD_P16 */
897 if ( ber_sockbuf_ctrl( sb, LBER_SB_OPT_DATA_READY, NULL ) ) {
900 return( -2 ); /* continue looking */
905 build_result_ber( LDAP *ld, BerElement **bp, LDAPRequest *lr )
913 ber = ldap_alloc_ber_with_options( ld );
916 ld->ld_errno = LDAP_NO_MEMORY;
920 if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
921 lr->lr_res_msgtype, lr->lr_res_errno,
922 lr->lr_res_matched ? lr->lr_res_matched : "",
923 lr->lr_res_error ? lr->lr_res_error : "" ) == -1 ) {
925 ld->ld_errno = LDAP_ENCODING_ERROR;
927 return( LBER_ERROR );
932 if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) {
933 ld->ld_errno = LDAP_DECODING_ERROR;
935 return( LBER_ERROR );
938 if ( ber_get_int( ber, &along ) == LBER_ERROR ) {
939 ld->ld_errno = LDAP_DECODING_ERROR;
941 return( LBER_ERROR );
944 tag = ber_peek_tag( ber, &len );
946 if ( tag == LBER_ERROR ) {
947 ld->ld_errno = LDAP_DECODING_ERROR;
949 return( LBER_ERROR );
958 merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
961 * Merge error information in "lr" with "parentr" error code and string.
963 if ( lr->lr_res_errno == LDAP_PARTIAL_RESULTS ) {
964 parentr->lr_res_errno = lr->lr_res_errno;
965 if ( lr->lr_res_error != NULL ) {
966 (void)ldap_append_referral( ld, &parentr->lr_res_error,
969 } else if ( lr->lr_res_errno != LDAP_SUCCESS &&
970 parentr->lr_res_errno == LDAP_SUCCESS ) {
971 parentr->lr_res_errno = lr->lr_res_errno;
972 if ( parentr->lr_res_error != NULL ) {
973 LDAP_FREE( parentr->lr_res_error );
975 parentr->lr_res_error = lr->lr_res_error;
976 lr->lr_res_error = NULL;
977 if ( LDAP_NAME_ERROR( lr->lr_res_errno )) {
978 if ( parentr->lr_res_matched != NULL ) {
979 LDAP_FREE( parentr->lr_res_matched );
981 parentr->lr_res_matched = lr->lr_res_matched;
982 lr->lr_res_matched = NULL;
987 LDAP_LOG( OPERATION, DETAIL1, "merged parent (id %d) error info: ",
988 parentr->lr_msgid, 0, 0 );
989 LDAP_LOG( OPERATION, DETAIL1, "result errno %d, error <%s>, matched <%s>\n",
990 parentr->lr_res_errno, parentr->lr_res_error ?
991 parentr->lr_res_error : "", parentr->lr_res_matched ?
992 parentr->lr_res_matched : "" );
994 Debug( LDAP_DEBUG_TRACE, "merged parent (id %d) error info: ",
995 parentr->lr_msgid, 0, 0 );
996 Debug( LDAP_DEBUG_TRACE, "result errno %d, error <%s>, matched <%s>\n",
997 parentr->lr_res_errno, parentr->lr_res_error ?
998 parentr->lr_res_error : "", parentr->lr_res_matched ?
999 parentr->lr_res_matched : "" );
1006 ldap_msgtype( LDAPMessage *lm )
1008 assert( lm != NULL );
1009 return ( lm != NULL ) ? lm->lm_msgtype : -1;
1014 ldap_msgid( LDAPMessage *lm )
1016 assert( lm != NULL );
1018 return ( lm != NULL ) ? lm->lm_msgid : -1;
1022 char * ldap_int_msgtype2str( ber_tag_t tag )
1025 case LDAP_RES_ADD: return "add";
1026 case LDAP_RES_BIND: return "bind";
1027 case LDAP_RES_COMPARE: return "compare";
1028 case LDAP_RES_DELETE: return "delete";
1029 case LDAP_RES_EXTENDED: return "extended-result";
1030 case LDAP_RES_EXTENDED_PARTIAL: return "extended-partial";
1031 case LDAP_RES_MODIFY: return "modify";
1032 case LDAP_RES_RENAME: return "rename";
1033 case LDAP_RES_SEARCH_ENTRY: return "search-entry";
1034 case LDAP_RES_SEARCH_REFERENCE: return "search-reference";
1035 case LDAP_RES_SEARCH_RESULT: return "search-result";
1041 ldap_msgfree( LDAPMessage *lm )
1047 LDAP_LOG ( OPERATION, ENTRY, "ldap_msgfree\n", 0, 0, 0 );
1049 Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
1052 for ( ; lm != NULL; lm = next ) {
1053 next = lm->lm_chain;
1054 type = lm->lm_msgtype;
1055 ber_free( lm->lm_ber, 1 );
1056 LDAP_FREE( (char *) lm );
1063 * ldap_msgdelete - delete a message. It returns:
1064 * 0 if the entire message was deleted
1065 * -1 if the message was not found, or only part of it was found
1068 ldap_msgdelete( LDAP *ld, int msgid )
1070 LDAPMessage *lm, *prev;
1072 assert( ld != NULL );
1075 LDAP_LOG ( OPERATION, ENTRY, "ldap_msgdelete\n", 0, 0, 0 );
1077 Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete\n", 0, 0, 0 );
1081 for ( lm = ld->ld_responses; lm != NULL; lm = lm->lm_next ) {
1082 if ( lm->lm_msgid == msgid )
1091 ld->ld_responses = lm->lm_next;
1093 prev->lm_next = lm->lm_next;
1095 if ( ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY )
1103 * return 1 if message msgid is waiting to be abandoned, 0 otherwise
1106 ldap_abandoned( LDAP *ld, ber_int_t msgid )
1110 if ( ld->ld_abandoned == NULL )
1113 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
1114 if ( ld->ld_abandoned[i] == msgid )
1122 ldap_mark_abandoned( LDAP *ld, ber_int_t msgid )
1126 if ( ld->ld_abandoned == NULL )
1129 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
1130 if ( ld->ld_abandoned[i] == msgid )
1133 if ( ld->ld_abandoned[i] == -1 )
1136 for ( ; ld->ld_abandoned[i] != -1; i++ ) {
1137 ld->ld_abandoned[i] = ld->ld_abandoned[i + 1];