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