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