]> git.sur5r.net Git - openldap/blob - libraries/libldap/result.c
Revert last change
[openldap] / libraries / libldap / result.c
1 /* result.c - wait for an ldap result */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2004 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
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>.
15  */
16 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
17  * All rights reserved.
18  */
19 /* This notice applies to changes, created by or for Novell, Inc.,
20  * to preexisting works for which notices appear elsewhere in this file.
21  *
22  * Copyright (C) 1999, 2000 Novell, Inc. All Rights Reserved.
23  *
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. 
32  *---
33  * Modification to OpenLDAP source by Novell, Inc.
34  * April 2000 sfs Add code to process V3 referrals and search results
35  *---
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.
39  */
40 /* Portions Copyright (C) The Internet Society (1997)
41  * ASN.1 fragments are from RFC 2251; see RFC for full legal notices.
42  */
43
44 /*
45  * LDAPv3 (RFC2251)
46  *      LDAPResult ::= SEQUENCE {
47  *              resultCode              ENUMERATED { ... },
48  *              matchedDN               LDAPDN,
49  *              errorMessage    LDAPString,
50  *              referral                Referral OPTIONAL
51  *      }
52  *      Referral ::= SEQUENCE OF LDAPURL        (one or more)
53  *      LDAPURL ::= LDAPString                          (limited to URL chars)
54  */
55
56 #include "portable.h"
57
58 #include <stdio.h>
59
60 #include <ac/stdlib.h>
61
62 #include <ac/errno.h>
63 #include <ac/socket.h>
64 #include <ac/string.h>
65 #include <ac/time.h>
66 #include <ac/unistd.h>
67
68 #include "ldap-int.h"
69 #include "ldap_log.h"
70
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));
80
81
82 /*
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.
96  *
97  * Example:
98  *      ldap_result( s, msgid, all, timeout, result )
99  */
100 int
101 ldap_result(
102         LDAP *ld,
103         int msgid,
104         int all,
105         struct timeval *timeout,
106         LDAPMessage **result )
107 {
108         LDAPMessage     *lm;
109         int     rc;
110
111         assert( ld != NULL );
112         assert( result != NULL );
113
114 #ifdef NEW_LOGGING
115         LDAP_LOG ( OPERATION, ARGS, "ldap_result msgid %d\n", msgid, 0, 0 );
116 #else
117         Debug( LDAP_DEBUG_TRACE, "ldap_result msgid %d\n", msgid, 0, 0 );
118 #endif
119
120 #ifdef LDAP_R_COMPILE
121         ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
122 #endif
123         lm = chkResponseList(ld, msgid, all);
124
125         if ( lm == NULL ) {
126                 rc = wait4msg( ld, msgid, all, timeout, result );
127         } else {
128                 *result = lm;
129                 ld->ld_errno = LDAP_SUCCESS;
130                 rc = lm->lm_msgtype;
131         }
132 #ifdef LDAP_R_COMPILE
133         ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
134 #endif
135         return( rc );
136 }
137
138 static LDAPMessage *
139 chkResponseList(
140         LDAP *ld,
141         int msgid,
142         int all)
143 {
144         LDAPMessage     *lm, *lastlm, *nextlm;
145     /*
146          * Look through the list of responses we have received on
147          * this association and see if the response we're interested in
148          * is there.  If it is, return it.  If not, call wait4msg() to
149          * wait until it arrives or timeout occurs.
150          */
151
152 #ifdef NEW_LOGGING
153         LDAP_LOG ( OPERATION, ARGS, "ldap_chkResponseList for msgid=%d, all=%d\n", 
154                 msgid, all, 0 );
155 #else
156         Debug( LDAP_DEBUG_TRACE,
157                 "ldap_chkResponseList for msgid=%d, all=%d\n",
158             msgid, all, 0 );
159 #endif
160         lastlm = NULL;
161         for ( lm = ld->ld_responses; lm != NULL; lm = nextlm ) {
162                 nextlm = lm->lm_next;
163
164                 if ( ldap_abandoned( ld, lm->lm_msgid ) ) {
165 #ifdef NEW_LOGGING
166                         LDAP_LOG ( OPERATION, DETAIL1, 
167                                 "ldap_chkResponseList msg abandoned, msgid %d\n", msgid, 0, 0 );
168 #else
169                         Debug( LDAP_DEBUG_TRACE,
170                                 "ldap_chkResponseList msg abandoned, msgid %d\n",
171                             msgid, 0, 0 );
172 #endif
173                         ldap_mark_abandoned( ld, lm->lm_msgid );
174
175                         if ( lastlm == NULL ) {
176                                 /* Remove first entry in list */
177                                 ld->ld_responses = lm->lm_next;
178                         } else {
179                                 lastlm->lm_next = nextlm;
180                         }
181
182                         ldap_msgfree( lm );
183
184                         continue;
185                 }
186
187                 if ( msgid == LDAP_RES_ANY || lm->lm_msgid == msgid ) {
188                         LDAPMessage     *tmp;
189
190                         if ( all == LDAP_MSG_ONE || msgid == LDAP_RES_UNSOLICITED ) {
191                                 break;
192                         }
193
194                         for ( tmp = lm; tmp != NULL; tmp = tmp->lm_chain ) {
195                                 if ( tmp->lm_msgtype != LDAP_RES_SEARCH_ENTRY
196                                     && tmp->lm_msgtype != LDAP_RES_SEARCH_REFERENCE
197                                         && tmp->lm_msgtype != LDAP_RES_INTERMEDIATE )
198                                 {
199                                         break;
200                                 }
201                         }
202
203                         if ( tmp == NULL ) {
204                                 lm = NULL;
205                         }
206
207                         break;
208                 }
209                 lastlm = lm;
210         }
211
212     if ( lm != NULL ) {
213                 /* Found an entry, remove it from the list */
214             if ( lastlm == NULL ) {
215                     ld->ld_responses = (all == LDAP_MSG_ONE && lm->lm_chain != NULL
216                         ? lm->lm_chain : lm->lm_next);
217             } else {
218                     lastlm->lm_next = (all == LDAP_MSG_ONE && lm->lm_chain != NULL
219                         ? lm->lm_chain : lm->lm_next);
220             }
221             if ( all == LDAP_MSG_ONE && lm->lm_chain != NULL ) {
222                     lm->lm_chain->lm_next = lm->lm_next;
223                     lm->lm_chain = NULL;
224             }
225             lm->lm_next = NULL;
226     }
227
228 #ifdef LDAP_DEBUG
229         if( lm == NULL) {
230 #ifdef NEW_LOGGING
231                 LDAP_LOG ( OPERATION, RESULTS, "ldap_chkResponseList returns NULL\n",
232                         0, 0, 0 );
233 #else
234                 Debug( LDAP_DEBUG_TRACE,
235                         "ldap_chkResponseList returns NULL\n", 0, 0, 0);
236 #endif
237         } else {
238 #ifdef NEW_LOGGING
239                 LDAP_LOG ( OPERATION, RESULTS, 
240                         "ldap_chkResponseList returns msgid %d, type 0x%02lu\n",
241                         lm->lm_msgid, (unsigned long) lm->lm_msgtype, 0 );
242 #else
243                 Debug( LDAP_DEBUG_TRACE,
244                         "ldap_chkResponseList returns msgid %d, type 0x%02lu\n",
245                         lm->lm_msgid, (unsigned long) lm->lm_msgtype, 0);
246 #endif
247         }
248 #endif
249     return lm;
250 }
251
252 static int
253 wait4msg(
254         LDAP *ld,
255         ber_int_t msgid,
256         int all,
257         struct timeval *timeout,
258         LDAPMessage **result )
259 {
260         int             rc;
261         struct timeval  tv, *tvp;
262         time_t          start_time = 0;
263         time_t          tmp_time;
264         LDAPConn        *lc, *nextlc;
265
266         assert( ld != NULL );
267         assert( result != NULL );
268
269 #ifdef LDAP_DEBUG
270         if ( timeout == NULL ) {
271 #ifdef NEW_LOGGING
272                 LDAP_LOG ( OPERATION, ARGS, 
273                         "wait4msg (infinite timeout), msgid %d\n", msgid, 0, 0 );
274 #else
275                 Debug( LDAP_DEBUG_TRACE, "wait4msg (infinite timeout), msgid %d\n",
276                     msgid, 0, 0 );
277 #endif
278         } else {
279 #ifdef NEW_LOGGING
280                 LDAP_LOG ( OPERATION, ARGS, 
281                         "wait4msg (timeout %ld sec, %ld usec), msgid %d\n", 
282                         (long) timeout->tv_sec, (long) timeout->tv_usec, msgid );
283 #else
284                 Debug( LDAP_DEBUG_TRACE, "wait4msg (timeout %ld sec, %ld usec), msgid %d\n",
285                        (long) timeout->tv_sec, (long) timeout->tv_usec, msgid );
286 #endif
287         }
288 #endif /* LDAP_DEBUG */
289
290         if ( timeout == NULL ) {
291                 tvp = NULL;
292         } else {
293                 tv = *timeout;
294                 tvp = &tv;
295                 start_time = time( NULL );
296         }
297                     
298         rc = -2;
299         while ( rc == -2 ) {
300 #ifdef LDAP_DEBUG
301 #ifdef NEW_LOGGING
302                 LDAP_LOG ( OPERATION, ARGS, 
303                         "wait4msg continue, msgid %d, all %d\n", msgid, all, 0 );
304 #else
305                 Debug( LDAP_DEBUG_TRACE, "wait4msg continue, msgid %d, all %d\n",
306                     msgid, all, 0 );
307 #endif
308                 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
309                         ldap_dump_connection( ld, ld->ld_conns, 1 );
310                         ldap_dump_requests_and_responses( ld );
311                 }
312 #endif /* LDAP_DEBUG */
313
314         if( (*result = chkResponseList(ld, msgid, all)) != NULL ) {
315             rc = (*result)->lm_msgtype;
316         } else {
317                         int lc_ready = 0;
318
319                         for ( lc = ld->ld_conns; lc != NULL; lc = nextlc ) {
320                                 nextlc = lc->lconn_next;
321                                 if ( ber_sockbuf_ctrl( lc->lconn_sb,
322                                                 LBER_SB_OPT_DATA_READY, NULL ) ) {
323                                         rc = try_read1msg( ld, msgid, all, lc->lconn_sb,
324                                                 &lc, result );
325                                         lc_ready = 1;
326                                     break;
327                                 }
328                 }
329
330                     if ( !lc_ready ) {
331                             rc = ldap_int_select( ld, tvp );
332 #ifdef LDAP_DEBUG
333                             if ( rc == -1 ) {
334 #ifdef NEW_LOGGING
335                                         LDAP_LOG ( OPERATION, ARGS, 
336                                                 "wait4msg: ldap_int_select returned -1: errno %d\n", 
337                                                 errno, 0, 0 );
338 #else
339                                 Debug( LDAP_DEBUG_TRACE,
340                                         "ldap_int_select returned -1: errno %d\n",
341                                         errno, 0, 0 );
342 #endif
343                             }
344 #endif
345
346                             if ( rc == 0 || ( rc == -1 && (
347                                     !LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART)
348                                     || errno != EINTR )))
349                             {
350                                     ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
351                                         LDAP_TIMEOUT);
352                                     return rc;
353                             }
354
355                             if ( rc == -1 ) {
356                                     rc = -2;    /* select interrupted: loop */
357                             } else {
358                                     rc = -2;
359 #ifdef LDAP_R_COMPILE
360                                     ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
361 #endif
362                                     if ( ld->ld_requests &&
363                                                 ld->ld_requests->lr_status == LDAP_REQST_WRITING &&
364                                                 ldap_is_write_ready( ld,
365                                                         ld->ld_requests->lr_conn->lconn_sb ) ) {
366                                                 ldap_int_flush_request( ld, ld->ld_requests );
367                                         }
368 #ifdef LDAP_R_COMPILE
369                                     ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
370 #endif
371                                     for ( lc = ld->ld_conns; rc == -2 && lc != NULL;
372                                         lc = nextlc ) {
373                                             nextlc = lc->lconn_next;
374                                             if ( lc->lconn_status ==
375                                                 LDAP_CONNST_CONNECTED &&
376                                                 ldap_is_read_ready( ld,
377                                                 lc->lconn_sb )) {
378                                                     rc = try_read1msg( ld, msgid, all,
379                                                         lc->lconn_sb, &lc, result );
380                                                         if ( lc == NULL ) lc = nextlc;
381                                             }
382                                     }
383                             }
384                     }
385                 }
386
387                 if ( rc == -2 && tvp != NULL ) {
388                         tmp_time = time( NULL );
389                         if (( tv.tv_sec -=  ( tmp_time - start_time )) <= 0 ) {
390                                 rc = 0; /* timed out */
391                                 ld->ld_errno = LDAP_TIMEOUT;
392                                 break;
393                         }
394
395 #ifdef NEW_LOGGING
396                         LDAP_LOG ( OPERATION, DETAIL1, 
397                                 "wait4msg: %ld secs to go\n", (long) tv.tv_sec, 0, 0 );
398 #else
399                         Debug( LDAP_DEBUG_TRACE, "wait4msg:  %ld secs to go\n",
400                                (long) tv.tv_sec, 0, 0 );
401 #endif
402                         start_time = tmp_time;
403                 }
404         }
405
406         return( rc );
407 }
408
409
410 static ber_tag_t
411 try_read1msg(
412         LDAP *ld,
413         ber_int_t msgid,
414         int all,
415         Sockbuf *sb,
416         LDAPConn **lcp,
417         LDAPMessage **result )
418 {
419         BerElement      *ber;
420         LDAPMessage     *new, *l, *prev, *tmp;
421         ber_int_t       id;
422         ber_tag_t       tag;
423         ber_len_t       len;
424         int             foundit = 0;
425         LDAPRequest     *lr, *tmplr;
426         LDAPConn        *lc;
427         BerElement      tmpber;
428         int             rc, refer_cnt, hadref, simple_request;
429         ber_int_t       lderr;
430 #ifdef LDAP_CONNECTIONLESS
431         int             firstmsg = 1, moremsgs = 0, isv2 = 0;
432 #endif
433         /*
434          * v3ref = flag for V3 referral / search reference
435          * 0 = not a ref, 1 = sucessfully chased ref, -1 = pass ref to application
436          */
437         int     v3ref;
438
439         assert( ld != NULL );
440         assert( lcp != NULL );
441         assert( *lcp != NULL );
442         
443 #ifdef NEW_LOGGING
444         LDAP_LOG ( OPERATION, ARGS, "read1msg: msgid %d, all %d\n", msgid, all, 0 );
445 #else
446         Debug( LDAP_DEBUG_TRACE, "read1msg: msgid %d, all %d\n", msgid, all, 0 );
447 #endif
448
449         lc = *lcp;
450
451 retry:
452         if ( lc->lconn_ber == NULL ) {
453                 lc->lconn_ber = ldap_alloc_ber_with_options(ld);
454
455                 if( lc->lconn_ber == NULL ) {
456                         return -1;
457                 }
458         }
459
460         ber = lc->lconn_ber;
461         assert( LBER_VALID (ber) );
462
463         /* get the next message */
464         errno = 0;
465 #ifdef LDAP_CONNECTIONLESS
466         if ( LDAP_IS_UDP(ld) ) {
467                 struct sockaddr from;
468                 ber_int_sb_read(sb, &from, sizeof(struct sockaddr));
469                 if (ld->ld_options.ldo_version == LDAP_VERSION2) isv2=1;
470         }
471 nextresp3:
472 #endif
473         tag = ber_get_next( sb, &len, ber );
474         if ( tag == LDAP_TAG_MESSAGE ) {
475                 /*
476                  * We read a complete message.
477                  * The connection should no longer need this ber.
478                  */
479                 lc->lconn_ber = NULL;
480         }
481         if ( tag != LDAP_TAG_MESSAGE ) {
482                 if ( tag == LBER_DEFAULT) {
483 #ifdef LDAP_DEBUG                  
484 #ifdef NEW_LOGGING
485                         LDAP_LOG ( OPERATION, DETAIL1, 
486                                 "read1msg: ber_get_next failed\n", 0, 0, 0 );
487 #else
488                         Debug( LDAP_DEBUG_CONNS,
489                               "ber_get_next failed.\n", 0, 0, 0 );
490 #endif             
491 #endif             
492 #ifdef EWOULDBLOCK                      
493                         if (errno==EWOULDBLOCK) return -2;
494 #endif
495 #ifdef EAGAIN
496                         if (errno == EAGAIN) return -2;
497 #endif
498                         ld->ld_errno = LDAP_SERVER_DOWN;
499                         return -1;
500                 }
501                 ld->ld_errno = LDAP_LOCAL_ERROR;
502                 return -1;
503         }
504
505         /* message id */
506         if ( ber_get_int( ber, &id ) == LBER_ERROR ) {
507                 ber_free( ber, 1 );
508                 ld->ld_errno = LDAP_DECODING_ERROR;
509                 return( -1 );
510         }
511
512         /* if it's been abandoned, toss it */
513         if ( ldap_abandoned( ld, id ) ) {
514 #ifdef NEW_LOGGING
515                 LDAP_LOG ( OPERATION, DETAIL1, "read1msg: abandoned\n", 0, 0, 0 );
516 #else
517                 Debug( LDAP_DEBUG_ANY, "abandoned\n", 0, 0, 0);
518 #endif
519 retry_ber:
520                 ber_free( ber, 1 );
521                 if ( ber_sockbuf_ctrl( sb, LBER_SB_OPT_DATA_READY, NULL ) ) {
522                         goto retry;
523                 }
524                 return( -2 );   /* continue looking */
525         }
526
527         if (( lr = ldap_find_request_by_msgid( ld, id )) == NULL ) {
528 #ifdef NEW_LOGGING
529                 LDAP_LOG ( OPERATION, DETAIL1, 
530                         "read1msg: no request for response with msgid %ld (tossing)\n",
531                         (long) id, 0, 0 );
532 #else
533                 Debug( LDAP_DEBUG_ANY,
534                     "no request for response with msgid %ld (tossing)\n",
535                     (long) id, 0, 0 );
536 #endif
537                 goto retry_ber;
538         }
539 #ifdef LDAP_CONNECTIONLESS
540         if (LDAP_IS_UDP(ld) && isv2) {
541                 ber_scanf(ber, "x{");
542         }
543 nextresp2:
544 #endif
545         /* the message type */
546         if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
547                 ld->ld_errno = LDAP_DECODING_ERROR;
548                 ber_free( ber, 1 );
549                 return( -1 );
550         }
551
552 #ifdef NEW_LOGGING
553         LDAP_LOG ( OPERATION, DETAIL1, 
554                 "read1msg: ldap_read: message type %s msgid %ld, original id %ld\n",
555             ldap_int_msgtype2str( tag ),
556                 (long) lr->lr_msgid, (long) lr->lr_origid );
557 #else
558         Debug( LDAP_DEBUG_TRACE,
559                 "ldap_read: message type %s msgid %ld, original id %ld\n",
560             ldap_int_msgtype2str( tag ),
561                 (long) lr->lr_msgid, (long) lr->lr_origid );
562 #endif
563
564         id = lr->lr_origid;
565         refer_cnt = 0;
566         hadref = simple_request = 0;
567         rc = -2;        /* default is to keep looking (no response found) */
568         lr->lr_res_msgtype = tag;
569
570         /*
571          * This code figures out if we are going to chase a
572          * referral / search reference, or pass it back to the application
573          */
574         v3ref = 0;      /* Assume not a V3 search reference or referral */
575         if( (tag != LDAP_RES_SEARCH_ENTRY) && (ld->ld_version > LDAP_VERSION2) ) {
576                 BerElement      tmpber = *ber;  /* struct copy */
577                 char **refs = NULL;
578
579                 if( tag == LDAP_RES_SEARCH_REFERENCE) {
580                         /* This is a V3 search reference */
581                         /* Assume we do not chase the reference, but pass it to application */
582                         v3ref = -1;
583                         if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ||
584                                         (lr->lr_parent != NULL) )
585                         {
586                                 /* Get the referral list */
587                                 if ( ber_scanf( &tmpber, "{v}", &refs ) == LBER_ERROR ) {
588                                         rc = LDAP_DECODING_ERROR;
589                                 } else {
590                                         /* Note: refs arrary is freed by ldap_chase_v3referrals */
591                                         refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
592                                             1, &lr->lr_res_error, &hadref );
593                                         if ( refer_cnt > 0 ) {  /* sucessfully chased reference */
594                                                 /* If haven't got end search, set chasing referrals */
595                                                 if( lr->lr_status != LDAP_REQST_COMPLETED) {
596                                                         lr->lr_status = LDAP_REQST_CHASINGREFS;
597 #ifdef NEW_LOGGING
598                                                         LDAP_LOG ( OPERATION, DETAIL1, 
599                                                                 "read1msg: search ref chased,"
600                                                                 "mark request chasing refs, id =        %d\n",
601                                                                 lr->lr_msgid, 0, 0 );
602 #else
603                                                         Debug( LDAP_DEBUG_TRACE,
604                                                             "read1msg:  search ref chased, mark request chasing refs, id = %d\n",
605                                                             lr->lr_msgid, 0, 0);
606 #endif
607                                                 }
608                                                 v3ref = 1;      /* We sucessfully chased the reference */
609                                         }
610                                 }
611                         }
612                 } else {
613                         /* Check for V3 referral */
614                         ber_len_t len;
615                         if ( ber_scanf( &tmpber, "{iaa",/*}*/ &lderr,
616                                     &lr->lr_res_matched, &lr->lr_res_error )
617                                     != LBER_ERROR ) {
618                                 /* Check if V3 referral */
619                                 if( ber_peek_tag( &tmpber, &len) == LDAP_TAG_REFERRAL ) {
620                                         /* We have a V3 referral, assume we cannot chase it */
621                                         v3ref = -1;
622                                         if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS)
623                                                          || (lr->lr_parent != NULL) )
624                                         {
625                                                 v3ref = -1;  /* Assume referral not chased and return it to app */
626                                                 /* Get the referral list */
627                                                 if( ber_scanf( &tmpber, "{v}", &refs) == LBER_ERROR) {
628                                                         rc = LDAP_DECODING_ERROR;
629                                                         lr->lr_status = LDAP_REQST_COMPLETED;
630 #ifdef NEW_LOGGING
631                                                         LDAP_LOG ( OPERATION, DETAIL1, 
632                                                                 "read1msg: referral decode error,"
633                                                                 "mark request completed, id =   %d\n",
634                                                                 lr->lr_msgid, 0, 0 );
635 #else
636                                                         Debug( LDAP_DEBUG_TRACE,
637                                                             "read1msg: referral decode error, mark request completed, id = %d\n",
638                                                                     lr->lr_msgid, 0, 0);
639 #endif
640                                                 } else {
641                                                         /* Chase the referral 
642                                                          * Note: refs arrary is freed by ldap_chase_v3referrals
643                                                          */
644                                                         refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
645                                                             0, &lr->lr_res_error, &hadref );
646                                                         lr->lr_status = LDAP_REQST_COMPLETED;
647 #ifdef NEW_LOGGING
648                                                         LDAP_LOG ( OPERATION, DETAIL1, 
649                                                                 "read1msg: referral chased,"
650                                                                 "mark request completed, id =   %d\n",
651                                                                 lr->lr_msgid, 0, 0 );
652 #else
653                                                         Debug( LDAP_DEBUG_TRACE,
654                                                             "read1msg:  referral chased, mark request completed, id = %d\n",
655                                                             lr->lr_msgid, 0, 0);
656 #endif
657                                                         if( refer_cnt > 0) {
658                                                                 v3ref = 1;  /* Referral successfully chased */
659                                                         }
660                                                 }
661                                         }
662                                 }
663
664                                 if( lr->lr_res_matched != NULL ) {
665                                         LDAP_FREE( lr->lr_res_matched );
666                                         lr->lr_res_matched = NULL;
667                                 }
668                                 if( lr->lr_res_error != NULL ) {
669                                         LDAP_FREE( lr->lr_res_error );
670                                         lr->lr_res_error = NULL;
671                                 }
672                         }
673                 }
674         }
675
676         /* All results that just return a status, i.e. don't return data
677          * go through the following code.  This code also chases V2 referrals
678          * and checks if all referrals have been chased.
679          */
680         if ( (tag != LDAP_RES_SEARCH_ENTRY) && (v3ref > -1) &&
681                 (tag != LDAP_RES_INTERMEDIATE ))
682         {
683                 /* For a v3 search referral/reference, only come here if already chased it */
684                 if ( ld->ld_version >= LDAP_VERSION2 &&
685                         ( lr->lr_parent != NULL ||
686                         LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ) )
687                 {
688                         tmpber = *ber;  /* struct copy */
689                         if ( v3ref == 1 ) {
690                                 /* V3 search reference or V3 referral
691                                  * sucessfully chased. If this message
692                                  * is a search result, then it has no more
693                                  * outstanding referrals.
694                                  */
695                                 if ( tag == LDAP_RES_SEARCH_RESULT )
696                                         refer_cnt = 0;
697                         } else if ( ber_scanf( &tmpber, "{iaa}", &lderr,
698                             &lr->lr_res_matched, &lr->lr_res_error )
699                             != LBER_ERROR ) {
700                                 if ( lderr != LDAP_SUCCESS ) {
701                                         /* referrals are in error string */
702                                         refer_cnt = ldap_chase_referrals( ld, lr,
703                                                 &lr->lr_res_error, -1, &hadref );
704                                         lr->lr_status = LDAP_REQST_COMPLETED;
705 #ifdef NEW_LOGGING
706                                         LDAP_LOG ( OPERATION, DETAIL1, 
707                                                 "read1msg: V2 referral chased,"
708                                                 "mark request completed, id =   %d\n",
709                                                 lr->lr_msgid, 0, 0 );
710 #else
711                                         Debug( LDAP_DEBUG_TRACE,
712                                             "read1msg:  V2 referral chased, mark request completed, id = %d\n", lr->lr_msgid, 0, 0);
713 #endif
714                                 }
715
716                                 /* save errno, message, and matched string */
717                                 if ( !hadref || lr->lr_res_error == NULL ) {
718                                         lr->lr_res_errno = ( lderr ==
719                                         LDAP_PARTIAL_RESULTS ) ? LDAP_SUCCESS
720                                         : lderr;
721                                 } else if ( ld->ld_errno != LDAP_SUCCESS ) {
722                                         lr->lr_res_errno = ld->ld_errno;
723                                 } else {
724                                         lr->lr_res_errno = LDAP_PARTIAL_RESULTS;
725                                 }
726 #ifdef NEW_LOGGING
727 LDAP_LOG ( OPERATION, DETAIL1, 
728         "read1msg: new result: res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
729     lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
730     lr->lr_res_matched ? lr->lr_res_matched : "" );
731 #else
732 Debug( LDAP_DEBUG_TRACE,
733     "new result:  res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
734     lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
735     lr->lr_res_matched ? lr->lr_res_matched : "" );
736 #endif
737                         }
738                 }
739
740 #ifdef NEW_LOGGING
741                 LDAP_LOG ( OPERATION, DETAIL1, "read1msg: %d new referrals\n", 
742                         refer_cnt, 0, 0 );
743 #else
744                 Debug( LDAP_DEBUG_TRACE,
745                     "read1msg:  %d new referrals\n", refer_cnt, 0, 0 );
746 #endif
747
748                 if ( refer_cnt != 0 ) { /* chasing referrals */
749                         ber_free( ber, 1 );
750                         ber = NULL;
751                         if ( refer_cnt < 0 ) {
752                                 return( -1 );   /* fatal error */
753                         }
754                         lr->lr_res_errno = LDAP_SUCCESS; /* sucessfully chased referral */
755                 } else {
756                         if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
757                                 /* request without any referrals */
758                                 simple_request = ( hadref ? 0 : 1 );
759                         } else {
760                                 /* request with referrals or child request */
761                                 ber_free( ber, 1 );
762                                 ber = NULL;
763                         }
764
765                         lr->lr_status = LDAP_REQST_COMPLETED; /* declare this request done */
766 #ifdef NEW_LOGGING
767                         LDAP_LOG ( OPERATION, DETAIL1, 
768                                 "read1msg: mark request completed, id = %d\n", 
769                                 lr->lr_msgid, 0, 0 );
770 #else
771                         Debug( LDAP_DEBUG_TRACE,
772                             "read1msg:  mark request completed, id = %d\n", lr->lr_msgid, 0, 0);
773 #endif
774                         while ( lr->lr_parent != NULL ) {
775                                 merge_error_info( ld, lr->lr_parent, lr );
776
777                                 lr = lr->lr_parent;
778                                 if ( --lr->lr_outrefcnt > 0 ) {
779                                         break;  /* not completely done yet */
780                                 }
781                         }
782
783                         /* Check if all requests are finished, lr is now parent */
784                         tmplr = lr;
785                         if (tmplr->lr_status == LDAP_REQST_COMPLETED) {
786                                 for(tmplr=lr->lr_child; tmplr != NULL; tmplr=tmplr->lr_refnext) {
787                                 if( tmplr->lr_status != LDAP_REQST_COMPLETED) {
788                                         break;
789                                         }
790                                 }
791                         }
792
793                         /* This is the parent request if the request has referrals */
794                         if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL && tmplr == NULL ) {
795                                 id = lr->lr_msgid;
796                                 tag = lr->lr_res_msgtype;
797 #ifdef NEW_LOGGING
798                         LDAP_LOG ( OPERATION, DETAIL1, 
799                                 "read1msg: request %ld done\n", (long) id, 0, 0 );
800                         LDAP_LOG ( OPERATION, DETAIL1, 
801                                 "read1msg: res_errno: %d,res_error: <%s>, res_matched: <%s>\n",
802                                 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
803                                 lr->lr_res_matched ? lr->lr_res_matched : "" );
804 #else
805                                 Debug( LDAP_DEBUG_ANY, "request %ld done\n",
806                                     (long) id, 0, 0 );
807 Debug( LDAP_DEBUG_TRACE,
808 "res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
809 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
810 lr->lr_res_matched ? lr->lr_res_matched : "" );
811 #endif
812                                 if ( !simple_request ) {
813                                         ber_free( ber, 1 );
814                                         ber = NULL;
815                                         if ( build_result_ber( ld, &ber, lr )
816                                             == LBER_ERROR ) {
817                                                 rc = -1; /* fatal error */
818                                         }
819                                 }
820
821                                 ldap_free_request( ld, lr );
822                         }
823
824                         if ( lc != NULL ) {
825                                 ldap_free_connection( ld, lc, 0, 1 );
826                                 *lcp = NULL;
827                         }
828                 }
829         }
830
831         if ( ber == NULL ) {
832                 return( rc );
833         }
834
835         /* make a new ldap message */
836         if ( (new = (LDAPMessage *) LDAP_CALLOC( 1, sizeof(LDAPMessage) ))
837             == NULL ) {
838                 ld->ld_errno = LDAP_NO_MEMORY;
839                 return( -1 );
840         }
841         new->lm_msgid = (int)id;
842         new->lm_msgtype = tag;
843         new->lm_ber = ber;
844
845 #ifdef LDAP_CONNECTIONLESS
846         /* CLDAP replies all fit in a single datagram. In LDAPv2 RFC1798
847          * the responses are all a sequence wrapped in one message. In
848          * LDAPv3 each response is in its own message. The datagram must
849          * end with a SearchResult. We can't just parse each response in
850          * separate calls to try_read1msg because the header info is only
851          * present at the beginning of the datagram, not at the beginning
852          * of each response. So parse all the responses at once and queue
853          * them up, then pull off the first response to return to the
854          * caller when all parsing is complete.
855          */
856         if ( LDAP_IS_UDP(ld) ) {
857                 /* If not a result, look for more */
858                 if ( tag != LDAP_RES_SEARCH_RESULT ) {
859                         int ok = 0;
860                         moremsgs = 1;
861                         if (isv2) {
862                                 /* LDAPv2: dup the current ber, skip past the current
863                                  * response, and see if there are any more after it.
864                                  */
865                                 ber = ber_dup( ber );
866                                 ber_scanf( ber, "x" );
867                                 if (ber_peek_tag(ber, &len) != LBER_DEFAULT) {
868                                         /* There's more - dup the ber buffer so they can all be
869                                          * individually freed by ldap_msgfree.
870                                          */
871                                         struct berval bv;
872                                         ber_get_option(ber, LBER_OPT_BER_REMAINING_BYTES, &len);
873                                         bv.bv_val = LDAP_MALLOC(len);
874                                         if (bv.bv_val) {
875                                                 ok=1;
876                                                 ber_read(ber, bv.bv_val, len);
877                                                 bv.bv_len = len;
878                                                 ber_init2(ber, &bv, ld->ld_lberoptions );
879                                         }
880                                 }
881                         } else {
882                                 /* LDAPv3: Just allocate a new ber. Since this is a buffered
883                                  * datagram, if the sockbuf is readable we still have data
884                                  * to parse.
885                                  */
886                                 ber = ldap_alloc_ber_with_options(ld);
887                                 if (ber_sockbuf_ctrl(sb, LBER_SB_OPT_DATA_READY, NULL)) ok=1;
888                         }
889                         /* set up response chain */
890                         if ( firstmsg ) {
891                                 firstmsg = 0;
892                                 new->lm_next = ld->ld_responses;
893                                 ld->ld_responses = new;
894                         } else {
895                                 tmp->lm_chain = new;
896                         }
897                         tmp = new;
898                         /* "ok" means there's more to parse */
899                         if (ok) {
900                                 if (isv2) goto nextresp2;
901                                 else goto nextresp3;
902                         } else {
903                                 /* got to end of datagram without a SearchResult. Free
904                                  * our dup'd ber, but leave any buffer alone. For v2 case,
905                                  * the previous response is still using this buffer. For v3,
906                                  * the new ber has no buffer to free yet.
907                                  */
908                                 ber_free(ber, 0);
909                                 return -1;
910                         }
911                 } else if ( moremsgs ) {
912                 /* got search result, and we had multiple responses in 1 datagram.
913                  * stick the result onto the end of the chain, and then pull the
914                  * first response off the head of the chain.
915                  */
916                         tmp->lm_chain = new;
917                         *result = chkResponseList( ld, msgid, all );
918                         ld->ld_errno = LDAP_SUCCESS;
919                         return( (*result)->lm_msgtype );
920                 }
921         }
922 #endif
923
924         /* is this the one we're looking for? */
925         if ( msgid == LDAP_RES_ANY || id == msgid ) {
926                 if ( all == LDAP_MSG_ONE
927                     || (new->lm_msgtype != LDAP_RES_SEARCH_RESULT
928                     && new->lm_msgtype != LDAP_RES_SEARCH_ENTRY
929                     && new->lm_msgtype != LDAP_RES_SEARCH_REFERENCE) ) {
930                         *result = new;
931                         ld->ld_errno = LDAP_SUCCESS;
932                         return( tag );
933                 } else if ( new->lm_msgtype == LDAP_RES_SEARCH_RESULT) {
934                         foundit = 1;    /* return the chain later */
935                 }
936         }
937
938         /* 
939          * if not, we must add it to the list of responses.  if
940          * the msgid is already there, it must be part of an existing
941          * search response.
942          */
943
944         prev = NULL;
945         for ( l = ld->ld_responses; l != NULL; l = l->lm_next ) {
946                 if ( l->lm_msgid == new->lm_msgid )
947                         break;
948                 prev = l;
949         }
950
951         /* not part of an existing search response */
952         if ( l == NULL ) {
953                 if ( foundit ) {
954                         *result = new;
955                         goto exit;
956                 }
957
958                 new->lm_next = ld->ld_responses;
959                 ld->ld_responses = new;
960                 goto exit;
961         }
962
963 #ifdef NEW_LOGGING
964         LDAP_LOG ( OPERATION, DETAIL1, 
965                 "read1msg: adding response id %ld type %ld\n",
966                 (long) new->lm_msgid, (long) new->lm_msgtype, 0 );
967 #else
968         Debug( LDAP_DEBUG_TRACE, "adding response id %ld type %ld:\n",
969             (long) new->lm_msgid, (long) new->lm_msgtype, 0 );
970 #endif
971
972         /* part of a search response - add to end of list of entries */
973         for ( tmp = l; (tmp->lm_chain != NULL) &&
974                 ((tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY) ||
975                  (tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_REFERENCE) ||
976                          (tmp->lm_chain->lm_msgtype == LDAP_RES_INTERMEDIATE ));
977             tmp = tmp->lm_chain )
978                 ;       /* NULL */
979         tmp->lm_chain = new;
980
981         /* return the whole chain if that's what we were looking for */
982         if ( foundit ) {
983                 if ( prev == NULL )
984                         ld->ld_responses = l->lm_next;
985                 else
986                         prev->lm_next = l->lm_next;
987                 *result = l;
988         }
989
990 exit:
991         if ( foundit ) {
992                 ld->ld_errno = LDAP_SUCCESS;
993                 return( tag );
994         }
995         if ( ber_sockbuf_ctrl( sb, LBER_SB_OPT_DATA_READY, NULL ) ) {
996                 goto retry;
997         }
998         return( -2 );   /* continue looking */
999 }
1000
1001
1002 static ber_tag_t
1003 build_result_ber( LDAP *ld, BerElement **bp, LDAPRequest *lr )
1004 {
1005         ber_len_t       len;
1006         ber_tag_t       tag;
1007         ber_int_t       along;
1008         BerElement *ber;
1009
1010         *bp = NULL;
1011         ber = ldap_alloc_ber_with_options( ld );
1012
1013         if( ber == NULL ) {
1014                 ld->ld_errno = LDAP_NO_MEMORY;
1015                 return LBER_ERROR;
1016         }
1017
1018         if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
1019             lr->lr_res_msgtype, lr->lr_res_errno,
1020             lr->lr_res_matched ? lr->lr_res_matched : "",
1021             lr->lr_res_error ? lr->lr_res_error : "" ) == -1 ) {
1022
1023                 ld->ld_errno = LDAP_ENCODING_ERROR;
1024                 ber_free(ber, 1);
1025                 return( LBER_ERROR );
1026         }
1027
1028         ber_reset( ber, 1 );
1029
1030         if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) {
1031                 ld->ld_errno = LDAP_DECODING_ERROR;
1032                 ber_free(ber, 1);
1033                 return( LBER_ERROR );
1034         }
1035
1036         if ( ber_get_int( ber, &along ) == LBER_ERROR ) {
1037                 ld->ld_errno = LDAP_DECODING_ERROR;
1038                 ber_free(ber, 1);
1039                 return( LBER_ERROR );
1040         }
1041
1042         tag = ber_peek_tag( ber, &len );
1043
1044         if ( tag == LBER_ERROR ) {
1045                 ld->ld_errno = LDAP_DECODING_ERROR;
1046                 ber_free(ber, 1);
1047                 return( LBER_ERROR );
1048         }
1049
1050         *bp = ber;
1051         return tag;
1052 }
1053
1054
1055 static void
1056 merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
1057 {
1058 /*
1059  * Merge error information in "lr" with "parentr" error code and string.
1060  */
1061         if ( lr->lr_res_errno == LDAP_PARTIAL_RESULTS ) {
1062                 parentr->lr_res_errno = lr->lr_res_errno;
1063                 if ( lr->lr_res_error != NULL ) {
1064                         (void)ldap_append_referral( ld, &parentr->lr_res_error,
1065                             lr->lr_res_error );
1066                 }
1067         } else if ( lr->lr_res_errno != LDAP_SUCCESS &&
1068             parentr->lr_res_errno == LDAP_SUCCESS ) {
1069                 parentr->lr_res_errno = lr->lr_res_errno;
1070                 if ( parentr->lr_res_error != NULL ) {
1071                         LDAP_FREE( parentr->lr_res_error );
1072                 }
1073                 parentr->lr_res_error = lr->lr_res_error;
1074                 lr->lr_res_error = NULL;
1075                 if ( LDAP_NAME_ERROR( lr->lr_res_errno )) {
1076                         if ( parentr->lr_res_matched != NULL ) {
1077                                 LDAP_FREE( parentr->lr_res_matched );
1078                         }
1079                         parentr->lr_res_matched = lr->lr_res_matched;
1080                         lr->lr_res_matched = NULL;
1081                 }
1082         }
1083
1084 #ifdef NEW_LOGGING
1085         LDAP_LOG( OPERATION, DETAIL1, "merged parent (id %d) error info:  ",
1086             parentr->lr_msgid, 0, 0 );
1087         LDAP_LOG( OPERATION, DETAIL1, "result errno %d, error <%s>, matched <%s>\n",
1088             parentr->lr_res_errno, parentr->lr_res_error ?
1089             parentr->lr_res_error : "", parentr->lr_res_matched ?
1090             parentr->lr_res_matched : "" );
1091 #else
1092         Debug( LDAP_DEBUG_TRACE, "merged parent (id %d) error info:  ",
1093             parentr->lr_msgid, 0, 0 );
1094         Debug( LDAP_DEBUG_TRACE, "result errno %d, error <%s>, matched <%s>\n",
1095             parentr->lr_res_errno, parentr->lr_res_error ?
1096             parentr->lr_res_error : "", parentr->lr_res_matched ?
1097             parentr->lr_res_matched : "" );
1098 #endif
1099 }
1100
1101
1102
1103 int
1104 ldap_msgtype( LDAPMessage *lm )
1105 {
1106         assert( lm != NULL );
1107         return ( lm != NULL ) ? lm->lm_msgtype : -1;
1108 }
1109
1110
1111 int
1112 ldap_msgid( LDAPMessage *lm )
1113 {
1114         assert( lm != NULL );
1115
1116         return ( lm != NULL ) ? lm->lm_msgid : -1;
1117 }
1118
1119
1120 char * ldap_int_msgtype2str( ber_tag_t tag )
1121 {
1122         switch( tag ) {
1123         case LDAP_RES_ADD: return "add";
1124         case LDAP_RES_BIND: return "bind";
1125         case LDAP_RES_COMPARE: return "compare";
1126         case LDAP_RES_DELETE: return "delete";
1127         case LDAP_RES_EXTENDED: return "extended-result";
1128         case LDAP_RES_INTERMEDIATE: return "intermediate";
1129         case LDAP_RES_MODIFY: return "modify";
1130         case LDAP_RES_RENAME: return "rename";
1131         case LDAP_RES_SEARCH_ENTRY: return "search-entry";
1132         case LDAP_RES_SEARCH_REFERENCE: return "search-reference";
1133         case LDAP_RES_SEARCH_RESULT: return "search-result";
1134         }
1135         return "unknown";
1136 }
1137
1138 int
1139 ldap_msgfree( LDAPMessage *lm )
1140 {
1141         LDAPMessage     *next;
1142         int             type = 0;
1143
1144 #ifdef NEW_LOGGING
1145         LDAP_LOG ( OPERATION, ENTRY, "ldap_msgfree\n", 0, 0, 0 );
1146 #else
1147         Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
1148 #endif
1149
1150         for ( ; lm != NULL; lm = next ) {
1151                 next = lm->lm_chain;
1152                 type = lm->lm_msgtype;
1153                 ber_free( lm->lm_ber, 1 );
1154                 LDAP_FREE( (char *) lm );
1155         }
1156
1157         return( type );
1158 }
1159
1160 /*
1161  * ldap_msgdelete - delete a message.  It returns:
1162  *      0       if the entire message was deleted
1163  *      -1      if the message was not found, or only part of it was found
1164  */
1165 int
1166 ldap_msgdelete( LDAP *ld, int msgid )
1167 {
1168         LDAPMessage     *lm, *prev;
1169         int rc = 0;
1170
1171         assert( ld != NULL );
1172
1173 #ifdef NEW_LOGGING
1174         LDAP_LOG ( OPERATION, ENTRY, "ldap_msgdelete\n", 0, 0, 0 );
1175 #else
1176         Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete\n", 0, 0, 0 );
1177 #endif
1178
1179         prev = NULL;
1180 #ifdef LDAP_R_COMPILE
1181         ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
1182 #endif
1183         for ( lm = ld->ld_responses; lm != NULL; lm = lm->lm_next ) {
1184                 if ( lm->lm_msgid == msgid )
1185                         break;
1186                 prev = lm;
1187         }
1188
1189         if ( lm == NULL ) {
1190                 rc = -1;
1191         } else {
1192                 if ( prev == NULL )
1193                         ld->ld_responses = lm->lm_next;
1194                 else
1195                         prev->lm_next = lm->lm_next;
1196         }
1197 #ifdef LDAP_R_COMPILE
1198         ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
1199 #endif
1200         if ( lm && ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY )
1201                 rc = -1;
1202
1203         return( rc );
1204 }
1205
1206
1207 /*
1208  * return 1 if message msgid is waiting to be abandoned, 0 otherwise
1209  */
1210 static int
1211 ldap_abandoned( LDAP *ld, ber_int_t msgid )
1212 {
1213         int     i;
1214
1215         if ( ld->ld_abandoned == NULL )
1216                 return( 0 );
1217
1218         for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
1219                 if ( ld->ld_abandoned[i] == msgid )
1220                         return( 1 );
1221
1222         return( 0 );
1223 }
1224
1225
1226 static int
1227 ldap_mark_abandoned( LDAP *ld, ber_int_t msgid )
1228 {
1229         int     i;
1230
1231         if ( ld->ld_abandoned == NULL )
1232                 return( -1 );
1233
1234         for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
1235                 if ( ld->ld_abandoned[i] == msgid )
1236                         break;
1237
1238         if ( ld->ld_abandoned[i] == -1 )
1239                 return( -1 );
1240
1241         for ( ; ld->ld_abandoned[i] != -1; i++ ) {
1242                 ld->ld_abandoned[i] = ld->ld_abandoned[i + 1];
1243         }
1244
1245         return( 0 );
1246 }