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