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