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