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