]> git.sur5r.net Git - openldap/blob - libraries/libldap/result.c
03a2677fe9166552be3b7a8a8ed0bb1cf0cc6881
[openldap] / libraries / libldap / result.c
1 /* result.c - wait for an ldap result */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2006 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
17  * All rights reserved.
18  */
19 /* This notice applies to changes, created by or for Novell, Inc.,
20  * to preexisting works for which notices appear elsewhere in this file.
21  *
22  * Copyright (C) 1999, 2000 Novell, Inc. All Rights Reserved.
23  *
24  * THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND TREATIES.
25  * USE, MODIFICATION, AND REDISTRIBUTION OF THIS WORK IS SUBJECT TO VERSION
26  * 2.0.1 OF THE OPENLDAP PUBLIC LICENSE, A COPY OF WHICH IS AVAILABLE AT
27  * HTTP://WWW.OPENLDAP.ORG/LICENSE.HTML OR IN THE FILE "LICENSE" IN THE
28  * TOP-LEVEL DIRECTORY OF THE DISTRIBUTION. ANY USE OR EXPLOITATION OF THIS
29  * WORK OTHER THAN AS AUTHORIZED IN VERSION 2.0.1 OF THE OPENLDAP PUBLIC
30  * LICENSE, OR OTHER PRIOR WRITTEN CONSENT FROM NOVELL, COULD SUBJECT THE
31  * PERPETRATOR TO CRIMINAL AND CIVIL LIABILITY. 
32  *---
33  * Modification to OpenLDAP source by Novell, Inc.
34  * April 2000 sfs Add code to process V3 referrals and search results
35  *---
36  * Note: A verbatim copy of version 2.0.1 of the OpenLDAP Public License 
37  * can be found in the file "build/LICENSE-2.0.1" in this distribution
38  * of OpenLDAP Software.
39  */
40 /* Portions Copyright (C) The Internet Society (1997)
41  * ASN.1 fragments are from RFC 2251; see RFC for full legal notices.
42  */
43
44 /*
45  * LDAPv3 (RFC2251)
46  *      LDAPResult ::= SEQUENCE {
47  *              resultCode              ENUMERATED { ... },
48  *              matchedDN               LDAPDN,
49  *              errorMessage    LDAPString,
50  *              referral                Referral OPTIONAL
51  *      }
52  *      Referral ::= SEQUENCE OF LDAPURL        (one or more)
53  *      LDAPURL ::= LDAPString                          (limited to URL chars)
54  */
55
56 #include "portable.h"
57
58 #include <stdio.h>
59
60 #include <ac/stdlib.h>
61
62 #include <ac/errno.h>
63 #include <ac/socket.h>
64 #include <ac/string.h>
65 #include <ac/time.h>
66 #include <ac/unistd.h>
67
68 #include "ldap-int.h"
69 #include "ldap_log.h"
70
71 static int ldap_abandoned LDAP_P(( LDAP *ld, ber_int_t msgid ));
72 static int ldap_mark_abandoned LDAP_P(( LDAP *ld, ber_int_t msgid ));
73 static int wait4msg LDAP_P(( LDAP *ld, ber_int_t msgid, int all, struct timeval *timeout,
74         LDAPMessage **result ));
75 static ber_tag_t try_read1msg LDAP_P(( LDAP *ld, ber_int_t msgid,
76         int all, LDAPConn **lc, LDAPMessage **result ));
77 static ber_tag_t build_result_ber LDAP_P(( LDAP *ld, BerElement **bp, LDAPRequest *lr ));
78 static void merge_error_info LDAP_P(( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr ));
79 static LDAPMessage * chkResponseList LDAP_P(( LDAP *ld, int msgid, int all));
80
81 #define LDAP_MSG_X_KEEP_LOOKING         (-2)
82
83
84 /*
85  * ldap_result - wait for an ldap result response to a message from the
86  * ldap server.  If msgid is LDAP_RES_ANY (-1), any message will be
87  * accepted.  If msgid is LDAP_RES_UNSOLICITED (0), any unsolicited
88  * message is accepted.  Otherwise ldap_result will wait for a response
89  * with msgid.  If all is LDAP_MSG_ONE (0) the first message with id
90  * msgid will be accepted, otherwise, ldap_result will wait for all
91  * responses with id msgid and then return a pointer to the entire list
92  * of messages.  In general, this is only useful for search responses,
93  * which can be of three message types (zero or more entries, zero or
94  * search references, followed by an ldap result).  An extension to
95  * LDAPv3 allows partial extended responses to be returned in response
96  * to any request.  The type of the first message received is returned.
97  * When waiting, any messages that have been abandoned are discarded.
98  *
99  * Example:
100  *      ldap_result( s, msgid, all, timeout, result )
101  */
102 int
103 ldap_result(
104         LDAP *ld,
105         int msgid,
106         int all,
107         struct timeval *timeout,
108         LDAPMessage **result )
109 {
110         LDAPMessage     *lm;
111         int     rc;
112
113         assert( ld != NULL );
114         assert( result != NULL );
115
116         Debug( LDAP_DEBUG_TRACE, "ldap_result ld %p msgid %d\n", (void *)ld, msgid, 0 );
117
118 #ifdef LDAP_R_COMPILE
119         ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
120 #endif
121         lm = chkResponseList(ld, msgid, all);
122
123         if ( lm == NULL ) {
124                 rc = wait4msg( ld, msgid, all, timeout, result );
125         } else {
126                 *result = lm;
127                 ld->ld_errno = LDAP_SUCCESS;
128                 rc = lm->lm_msgtype;
129         }
130 #ifdef LDAP_R_COMPILE
131         ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
132 #endif
133         return( rc );
134 }
135
136 static LDAPMessage *
137 chkResponseList(
138         LDAP *ld,
139         int msgid,
140         int all)
141 {
142         LDAPMessage     *lm, **lastlm, *nextlm;
143     /*
144          * 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                         tmp_time = time( NULL );
373                         tv0.tv_sec -= ( tmp_time - start_time );
374                         if ( tv0.tv_sec <= 0 ) {
375                                 rc = 0; /* timed out */
376                                 ld->ld_errno = LDAP_TIMEOUT;
377                                 break;
378                         }
379                         tv.tv_sec = tv0.tv_sec;
380
381                         Debug( LDAP_DEBUG_TRACE, "wait4msg ld %p %ld secs to go\n",
382                                 (void *)ld, (long) tv.tv_sec, 0 );
383                         start_time = tmp_time;
384                 }
385         }
386
387         return( rc );
388 }
389
390
391 static ber_tag_t
392 try_read1msg(
393         LDAP *ld,
394         ber_int_t msgid,
395         int all,
396         LDAPConn **lcp,
397         LDAPMessage **result )
398 {
399         BerElement      *ber;
400         LDAPMessage     *newmsg, *l, *prev;
401         ber_int_t       id;
402         ber_tag_t       tag;
403         ber_len_t       len;
404         int             foundit = 0;
405         LDAPRequest     *lr, *tmplr;
406         LDAPConn        *lc;
407         BerElement      tmpber;
408         int             rc, refer_cnt, hadref, simple_request;
409         ber_int_t       lderr;
410
411 #ifdef LDAP_CONNECTIONLESS
412         LDAPMessage     *tmp = NULL, *chain_head = NULL;
413         int             moremsgs = 0, isv2 = 0;
414 #endif
415
416         /*
417          * v3ref = flag for V3 referral / search reference
418          * 0 = not a ref, 1 = sucessfully chased ref, -1 = pass ref to application
419          */
420         enum {
421                 V3REF_NOREF     = 0,
422                 V3REF_SUCCESS   = 1,
423                 V3REF_TOAPP     = -1
424         }       v3ref;
425
426         assert( ld != NULL );
427         assert( lcp != NULL );
428         assert( *lcp != NULL );
429         
430         Debug( LDAP_DEBUG_TRACE, "read1msg: ld %p msgid %d all %d\n",
431                 (void *)ld, msgid, all );
432
433         lc = *lcp;
434
435 retry:
436         if ( lc->lconn_ber == NULL ) {
437                 lc->lconn_ber = ldap_alloc_ber_with_options(ld);
438
439                 if( lc->lconn_ber == NULL ) {
440                         return -1;
441                 }
442         }
443
444         ber = lc->lconn_ber;
445         assert( LBER_VALID (ber) );
446
447         /* get the next message */
448         sock_errset(0);
449 #ifdef LDAP_CONNECTIONLESS
450         if ( LDAP_IS_UDP(ld) ) {
451                 struct sockaddr from;
452                 ber_int_sb_read( lc->lconn_sb, &from, sizeof(struct sockaddr) );
453                 if (ld->ld_options.ldo_version == LDAP_VERSION2) isv2=1;
454         }
455 nextresp3:
456 #endif
457         tag = ber_get_next( lc->lconn_sb, &len, ber );
458         if ( tag == LDAP_TAG_MESSAGE ) {
459                 /*
460                  * We read a complete message.
461                  * The connection should no longer need this ber.
462                  */
463                 lc->lconn_ber = NULL;
464         }
465         if ( tag != LDAP_TAG_MESSAGE ) {
466                 if ( tag == LBER_DEFAULT) {
467 #ifdef LDAP_DEBUG                  
468                         Debug( LDAP_DEBUG_CONNS,
469                                 "ber_get_next failed.\n", 0, 0, 0 );
470 #endif             
471 #ifdef EWOULDBLOCK                      
472                         if ( sock_errno() == EWOULDBLOCK ) return LDAP_MSG_X_KEEP_LOOKING;
473 #endif
474 #ifdef EAGAIN
475                         if ( sock_errno() == EAGAIN ) return LDAP_MSG_X_KEEP_LOOKING;
476 #endif
477                         ld->ld_errno = LDAP_SERVER_DOWN;
478                         return -1;
479                 }
480                 ld->ld_errno = LDAP_LOCAL_ERROR;
481                 return -1;
482         }
483
484         /* message id */
485         if ( ber_get_int( ber, &id ) == LBER_ERROR ) {
486                 ber_free( ber, 1 );
487                 ld->ld_errno = LDAP_DECODING_ERROR;
488                 return( -1 );
489         }
490
491         /* if it's been abandoned, toss it */
492         if ( ldap_abandoned( ld, id ) ) {
493                 Debug( LDAP_DEBUG_ANY, "abandoned ld %p msgid %ld\n",
494                         (void *)ld, (long) id, 0);
495 retry_ber:
496                 ber_free( ber, 1 );
497                 if ( ber_sockbuf_ctrl( lc->lconn_sb, LBER_SB_OPT_DATA_READY, NULL ) ) {
498                         goto retry;
499                 }
500                 return( LDAP_MSG_X_KEEP_LOOKING );      /* continue looking */
501         }
502
503         lr = ldap_find_request_by_msgid( ld, id );
504         if ( lr == NULL ) {
505                 Debug( LDAP_DEBUG_ANY,
506                         "no request for response on ld %p msgid %ld (tossing)\n",
507                         (void *)ld, (long) id, 0 );
508                 goto retry_ber;
509         }
510 #ifdef LDAP_CONNECTIONLESS
511         if (LDAP_IS_UDP(ld) && isv2) {
512                 ber_scanf(ber, "x{");
513         }
514 nextresp2:
515 #endif
516         /* the message type */
517         if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
518                 ld->ld_errno = LDAP_DECODING_ERROR;
519                 ber_free( ber, 1 );
520                 return( -1 );
521         }
522
523         Debug( LDAP_DEBUG_TRACE,
524                 "read1msg: ld %p msgid %ld message type %s\n",
525                 (void *)ld, (long) lr->lr_msgid, ldap_int_msgtype2str( tag ));
526
527         id = lr->lr_origid;
528         refer_cnt = 0;
529         hadref = simple_request = 0;
530         rc = LDAP_MSG_X_KEEP_LOOKING;   /* default is to keep looking (no response found) */
531         lr->lr_res_msgtype = tag;
532
533         /*
534          * This code figures out if we are going to chase a
535          * referral / search reference, or pass it back to the application
536          */
537         v3ref = V3REF_NOREF;    /* Assume not a V3 search reference/referral */
538         if( (tag != LDAP_RES_SEARCH_ENTRY) && (ld->ld_version > LDAP_VERSION2) ) {
539                 BerElement      tmpber = *ber;  /* struct copy */
540                 char **refs = NULL;
541
542                 if( tag == LDAP_RES_SEARCH_REFERENCE ) {
543                         /* This is a V3 search reference */
544                         /* Assume we do not chase the reference,
545                          * but pass it to application */
546                         v3ref = V3REF_TOAPP;
547                         if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ||
548                                         (lr->lr_parent != NULL) )
549                         {
550                                 /* Get the referral list */
551                                 if ( ber_scanf( &tmpber, "{v}", &refs ) == LBER_ERROR ) {
552                                         rc = LDAP_DECODING_ERROR;
553                                 } else {
554                                         /* Note: refs array is freed by ldap_chase_v3referrals */
555                                         refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
556                                             1, &lr->lr_res_error, &hadref );
557                                         if ( refer_cnt > 0 ) {
558                                                 /* sucessfully chased reference */
559                                                 /* If haven't got end search, set chasing referrals */
560                                                 if( lr->lr_status != LDAP_REQST_COMPLETED) {
561                                                         lr->lr_status = LDAP_REQST_CHASINGREFS;
562                                                         Debug( LDAP_DEBUG_TRACE,
563                                                                 "read1msg:  search ref chased, "
564                                                                 "mark request chasing refs, "
565                                                                 "id = %d\n",
566                                                                 lr->lr_msgid, 0, 0);
567                                                 }
568
569                                                 /* We successfully chased the reference */
570                                                 v3ref = V3REF_SUCCESS;
571                                         }
572                                 }
573                         }
574                 } else {
575                         /* Check for V3 referral */
576                         ber_len_t       len;
577                         char            *lr_res_error = NULL;
578
579 #ifdef LDAP_NULL_IS_NULL
580                         if ( ber_scanf( &tmpber, "{eAA",/*}*/ &lderr,
581                                     &lr->lr_res_matched, &lr_res_error )
582                                     != LBER_ERROR )
583 #else /* ! LDAP_NULL_IS_NULL */
584                         if ( ber_scanf( &tmpber, "{eaa",/*}*/ &lderr,
585                                     &lr->lr_res_matched, &lr_res_error )
586                                     != LBER_ERROR )
587 #endif /* ! LDAP_NULL_IS_NULL */
588                         {
589                                 if ( lr_res_error != NULL ) {
590 #ifndef LDAP_NULL_IS_NULL
591                                         if ( lr_res_error[ 0 ] == '\0' ) {
592                                                 LDAP_FREE( lr_res_error );
593                                                 lr_res_error = NULL;
594                                         } else
595 #endif /* ! LDAP_NULL_IS_NULL */
596                                         {
597                                                 if ( lr->lr_res_error != NULL ) {
598                                                         (void)ldap_append_referral( ld, &lr->lr_res_error, lr_res_error );
599                                                         LDAP_FREE( (char *)lr_res_error );
600
601                                                 } else {
602                                                         lr->lr_res_error = lr_res_error;
603                                                 }
604                                         }
605                                         lr_res_error = NULL;
606                                 }
607
608                                 /* Check if V3 referral */
609                                 if ( ber_peek_tag( &tmpber, &len ) == LDAP_TAG_REFERRAL ) {
610                                         /* We have a V3 referral, assume we cannot chase it */
611                                         v3ref = V3REF_TOAPP;
612                                         if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS)
613                                                          || (lr->lr_parent != NULL) )
614                                         {
615                                                 /* Assume referral not chased and return it to app */
616                                                 v3ref = V3REF_TOAPP;
617
618                                                 /* Get the referral list */
619                                                 if( ber_scanf( &tmpber, "{v}", &refs) == LBER_ERROR) {
620                                                         rc = LDAP_DECODING_ERROR;
621                                                         lr->lr_status = LDAP_REQST_COMPLETED;
622                                                         Debug( LDAP_DEBUG_TRACE,
623                                                                 "read1msg: referral decode error, mark request completed, ld %p msgid %d\n",
624                                                                 (void *)ld, lr->lr_msgid, 0);
625                                                 } else {
626                                                         /* Chase the referral 
627                                                          * Note: refs arrary is freed by ldap_chase_v3referrals
628                                                          */
629                                                         refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
630                                                             0, &lr->lr_res_error, &hadref );
631                                                         lr->lr_status = LDAP_REQST_COMPLETED;
632                                                         Debug( LDAP_DEBUG_TRACE,
633                                                                 "read1msg: referral chased, mark request completed, ld %p msgid %d\n",
634                                                                 (void *)ld, lr->lr_msgid, 0);
635                                                         if( refer_cnt > 0) {
636                                                                 /* Referral successfully chased */
637                                                                 v3ref = V3REF_SUCCESS;
638                                                         }
639                                                 }
640                                         }
641                                 }
642
643                                 if( lr->lr_res_matched != NULL ) {
644                                         LDAP_FREE( lr->lr_res_matched );
645                                         lr->lr_res_matched = NULL;
646                                 }
647                                 if( lr->lr_res_error != NULL ) {
648                                         LDAP_FREE( lr->lr_res_error );
649                                         lr->lr_res_error = NULL;
650                                 }
651
652                                 /* Since it's not a SearchReference, it must be a
653                                  * result. Since we're not chasing the referral,
654                                  * this request is done.
655                                  */
656                                 if ( v3ref == V3REF_TOAPP ) {
657                                         lr->lr_status = LDAP_REQST_COMPLETED;
658                                         Debug( LDAP_DEBUG_TRACE,
659                                                 "request done: ld %p msgid %d, "
660                                                 "referral returned to app\n",
661                                                 (void *)ld, lr->lr_msgid, 0);
662 #ifdef LDAP_R_COMPILE
663                                         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
664 #endif
665                                         ldap_free_request( ld, lr );
666 #ifdef LDAP_R_COMPILE
667                                         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
668 #endif
669                                         lr = NULL;
670                                 }
671                         }
672                 }
673         }
674
675         /* All results that just return a status, i.e. don't return data
676          * go through the following code.  This code also chases V2 referrals
677          * and checks if all referrals have been chased.
678          */
679         if ( (tag != LDAP_RES_SEARCH_ENTRY) && (v3ref != V3REF_TOAPP) &&
680                 (tag != LDAP_RES_INTERMEDIATE ))
681         {
682                 /* For a v3 search referral/reference, only come here if already chased it */
683                 if ( ld->ld_version >= LDAP_VERSION2 &&
684                         ( lr->lr_parent != NULL ||
685                         LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ) )
686                 {
687                         char            *lr_res_error = NULL;
688
689                         tmpber = *ber;  /* struct copy */
690                         if ( v3ref == V3REF_SUCCESS ) {
691                                 /* V3 search reference or V3 referral
692                                  * sucessfully chased. If this message
693                                  * is a search result, then it has no more
694                                  * outstanding referrals.
695                                  */
696                                 if ( tag == LDAP_RES_SEARCH_RESULT )
697                                         refer_cnt = 0;
698 #ifdef LDAP_NULL_IS_NULL
699                         } else if ( ber_scanf( &tmpber, "{eAA}", &lderr,
700                                 &lr->lr_res_matched, &lr_res_error )
701                                 != LBER_ERROR )
702 #else /* ! LDAP_NULL_IS_NULL */
703                         } else if ( ber_scanf( &tmpber, "{eaa}", &lderr,
704                                 &lr->lr_res_matched, &lr_res_error )
705                                 != LBER_ERROR )
706 #endif /* ! LDAP_NULL_IS_NULL */
707                         {
708                                 if ( lr_res_error != NULL ) {
709 #ifndef LDAP_NULL_IS_NULL
710                                         if ( lr_res_error[ 0 ] == '\0' ) {
711                                                 LDAP_FREE( lr_res_error );
712                                         } else
713 #endif /* ! LDAP_NULL_IS_NULL */
714                                         {
715                                                 if ( lr->lr_res_error != NULL ) {
716                                                         (void)ldap_append_referral( ld, &lr->lr_res_error, lr_res_error );
717                                                         LDAP_FREE( (char *)lr_res_error );
718                                                 } else {
719                                                         lr->lr_res_error = lr_res_error;
720                                                 }
721                                         }
722                                         lr_res_error = NULL;
723                                 }
724
725                                 switch ( lderr ) {
726                                 case LDAP_SUCCESS:
727                                 case LDAP_COMPARE_TRUE:
728                                 case LDAP_COMPARE_FALSE:
729                                         break;
730
731                                 default:
732                                         if ( lr->lr_res_error == NULL
733                                                 || lr->lr_res_error[ 0 ] == '\0' )
734                                         {
735                                                 break;
736                                         }
737
738                                         /* referrals are in error string */
739                                         refer_cnt = ldap_chase_referrals( ld, lr,
740                                                 &lr->lr_res_error, -1, &hadref );
741                                         lr->lr_status = LDAP_REQST_COMPLETED;
742                                         Debug( LDAP_DEBUG_TRACE,
743                                                 "read1msg:  V2 referral chased, "
744                                                 "mark request completed, id = %d\n",
745                                                 lr->lr_msgid, 0, 0 );
746                                         break;
747                                 }
748
749                                 /* save errno, message, and matched string */
750                                 if ( !hadref || lr->lr_res_error == NULL ) {
751                                         lr->lr_res_errno = ( lderr ==
752                                         LDAP_PARTIAL_RESULTS ) ? LDAP_SUCCESS
753                                         : lderr;
754                                 } else if ( ld->ld_errno != LDAP_SUCCESS ) {
755                                         lr->lr_res_errno = ld->ld_errno;
756                                 } else {
757                                         lr->lr_res_errno = LDAP_PARTIAL_RESULTS;
758                                 }
759
760                                 Debug( LDAP_DEBUG_TRACE, "new result:  "
761                                         "res_errno: %d, "
762                                         "res_error: <%s>, "
763                                         "res_matched: <%s>\n",
764                                         lr->lr_res_errno,
765                                         lr->lr_res_error ? lr->lr_res_error : "",
766                                         lr->lr_res_matched ? lr->lr_res_matched : "" );
767                         }
768
769                         /* in any case, don't leave any lr_res_error 'round */
770                         if ( lr_res_error ) {
771                                 LDAP_FREE( lr_res_error );
772                         }
773                 }
774
775                 Debug( LDAP_DEBUG_TRACE,
776                         "read1msg: ld %p %d new referrals\n",
777                         (void *)ld, refer_cnt, 0 );
778
779                 if ( refer_cnt != 0 ) { /* chasing referrals */
780                         ber_free( ber, 1 );
781                         ber = NULL;
782                         if ( refer_cnt < 0 ) {
783                                 return( -1 );   /* fatal error */
784                         }
785                         lr->lr_res_errno = LDAP_SUCCESS; /* sucessfully chased referral */
786                 } else {
787                         if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
788                                 /* request without any referrals */
789                                 simple_request = ( hadref ? 0 : 1 );
790                         } else {
791                                 /* request with referrals or child request */
792                                 ber_free( ber, 1 );
793                                 ber = NULL;
794                         }
795
796                         lr->lr_status = LDAP_REQST_COMPLETED; /* declare this request done */
797                         Debug( LDAP_DEBUG_TRACE,
798                                 "read1msg:  mark request completed, ld %p msgid %d\n",
799                                 (void *)ld, lr->lr_msgid, 0);
800                         while ( lr->lr_parent != NULL ) {
801                                 merge_error_info( ld, lr->lr_parent, lr );
802
803                                 lr = lr->lr_parent;
804                                 if ( --lr->lr_outrefcnt > 0 ) {
805                                         break;  /* not completely done yet */
806                                 }
807                         }
808
809                         /* Check if all requests are finished, lr is now parent */
810                         tmplr = lr;
811                         if (tmplr->lr_status == LDAP_REQST_COMPLETED) {
812                                 for ( tmplr=lr->lr_child;
813                                         tmplr != NULL;
814                                         tmplr=tmplr->lr_refnext)
815                                 {
816                                         if( tmplr->lr_status != LDAP_REQST_COMPLETED) break;
817                                 }
818                         }
819
820                         /* This is the parent request if the request has referrals */
821                         if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL &&
822                                 tmplr == NULL )
823                         {
824                                 id = lr->lr_msgid;
825                                 tag = lr->lr_res_msgtype;
826                                 Debug( LDAP_DEBUG_ANY, "request done: ld %p msgid %ld\n",
827                                         (void *)ld, (long) id, 0 );
828 Debug( LDAP_DEBUG_TRACE,
829 "res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
830 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
831 lr->lr_res_matched ? lr->lr_res_matched : "" );
832                                 if ( !simple_request ) {
833                                         ber_free( ber, 1 );
834                                         ber = NULL;
835                                         if ( build_result_ber( ld, &ber, lr )
836                                             == LBER_ERROR ) {
837                                                 rc = -1; /* fatal error */
838                                         }
839                                 }
840
841 #ifdef LDAP_R_COMPILE
842                                 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
843 #endif
844                                 ldap_free_request( ld, lr );
845 #ifdef LDAP_R_COMPILE
846                                 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
847 #endif
848                         }
849
850                         if ( lc != NULL ) {
851 #ifdef LDAP_R_COMPILE
852                                 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
853 #endif
854                                 ldap_free_connection( ld, lc, 0, 1 );
855 #ifdef LDAP_R_COMPILE
856                                 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
857 #endif
858                                 lc = *lcp = NULL;
859                         }
860                 }
861         }
862
863         if ( ber == NULL ) {
864                 return( rc );
865         }
866
867         /* make a new ldap message */
868         newmsg = (LDAPMessage *) LDAP_CALLOC( 1, sizeof(LDAPMessage) );
869         if ( newmsg == NULL ) {
870                 ld->ld_errno = LDAP_NO_MEMORY;
871                 return( -1 );
872         }
873         newmsg->lm_msgid = (int)id;
874         newmsg->lm_msgtype = tag;
875         newmsg->lm_ber = ber;
876         newmsg->lm_chain_tail = newmsg;
877
878 #ifdef LDAP_CONNECTIONLESS
879         /* CLDAP replies all fit in a single datagram. In LDAPv2 RFC1798
880          * the responses are all a sequence wrapped in one message. In
881          * LDAPv3 each response is in its own message. The datagram must
882          * end with a SearchResult. We can't just parse each response in
883          * separate calls to try_read1msg because the header info is only
884          * present at the beginning of the datagram, not at the beginning
885          * of each response. So parse all the responses at once and queue
886          * them up, then pull off the first response to return to the
887          * caller when all parsing is complete.
888          */
889         if ( LDAP_IS_UDP(ld) ) {
890                 /* If not a result, look for more */
891                 if ( tag != LDAP_RES_SEARCH_RESULT ) {
892                         int ok = 0;
893                         moremsgs = 1;
894                         if (isv2) {
895                                 /* LDAPv2: dup the current ber, skip past the current
896                                  * response, and see if there are any more after it.
897                                  */
898                                 ber = ber_dup( ber );
899                                 ber_scanf( ber, "x" );
900                                 if (ber_peek_tag(ber, &len) != LBER_DEFAULT) {
901                                         /* There's more - dup the ber buffer so they can all be
902                                          * individually freed by ldap_msgfree.
903                                          */
904                                         struct berval bv;
905                                         ber_get_option(ber, LBER_OPT_BER_REMAINING_BYTES, &len);
906                                         bv.bv_val = LDAP_MALLOC(len);
907                                         if (bv.bv_val) {
908                                                 ok=1;
909                                                 ber_read(ber, bv.bv_val, len);
910                                                 bv.bv_len = len;
911                                                 ber_init2(ber, &bv, ld->ld_lberoptions );
912                                         }
913                                 }
914                         } else {
915                                 /* LDAPv3: Just allocate a new ber. Since this is a buffered
916                                  * datagram, if the sockbuf is readable we still have data
917                                  * to parse.
918                                  */
919                                 ber = ldap_alloc_ber_with_options( ld );
920                                 if ( ber_sockbuf_ctrl( lc->lconn_sb, LBER_SB_OPT_DATA_READY, NULL ) ) ok = 1;
921                         }
922                         /* set up response chain */
923                         if ( tmp == NULL ) {
924                                 newmsg->lm_next = ld->ld_responses;
925                                 ld->ld_responses = newmsg;
926                                 chain_head = newmsg;
927                         } else {
928                                 tmp->lm_chain = newmsg;
929                         }
930                         chain_head->lm_chain_tail = newmsg;
931                         tmp = newmsg;
932                         /* "ok" means there's more to parse */
933                         if (ok) {
934                                 if (isv2) goto nextresp2;
935                                 else goto nextresp3;
936                         } else {
937                                 /* got to end of datagram without a SearchResult. Free
938                                  * our dup'd ber, but leave any buffer alone. For v2 case,
939                                  * the previous response is still using this buffer. For v3,
940                                  * the new ber has no buffer to free yet.
941                                  */
942                                 ber_free(ber, 0);
943                                 return -1;
944                         }
945                 } else if ( moremsgs ) {
946                 /* got search result, and we had multiple responses in 1 datagram.
947                  * stick the result onto the end of the chain, and then pull the
948                  * first response off the head of the chain.
949                  */
950                         tmp->lm_chain = newmsg;
951                         chain_head->lm_chain_tail = newmsg;
952                         *result = chkResponseList( ld, msgid, all );
953                         ld->ld_errno = LDAP_SUCCESS;
954                         return( (*result)->lm_msgtype );
955                 }
956         }
957 #endif /* LDAP_CONNECTIONLESS */
958
959         /* is this the one we're looking for? */
960         if ( msgid == LDAP_RES_ANY || id == msgid ) {
961                 if ( all == LDAP_MSG_ONE
962                     || (newmsg->lm_msgtype != LDAP_RES_SEARCH_RESULT
963                     && newmsg->lm_msgtype != LDAP_RES_SEARCH_ENTRY
964                     && newmsg->lm_msgtype != LDAP_RES_SEARCH_REFERENCE) ) {
965                         *result = newmsg;
966                         ld->ld_errno = LDAP_SUCCESS;
967                         return( tag );
968                 } else if ( newmsg->lm_msgtype == LDAP_RES_SEARCH_RESULT) {
969                         foundit = 1;    /* return the chain later */
970                 }
971         }
972
973         /* 
974          * if not, we must add it to the list of responses.  if
975          * the msgid is already there, it must be part of an existing
976          * search response.
977          */
978
979         prev = NULL;
980         for ( l = ld->ld_responses; l != NULL; l = l->lm_next ) {
981                 if ( l->lm_msgid == newmsg->lm_msgid )
982                         break;
983                 prev = l;
984         }
985
986         /* not part of an existing search response */
987         if ( l == NULL ) {
988                 if ( foundit ) {
989                         *result = newmsg;
990                         goto exit;
991                 }
992
993                 newmsg->lm_next = ld->ld_responses;
994                 ld->ld_responses = newmsg;
995                 goto exit;
996         }
997
998         Debug( LDAP_DEBUG_TRACE, "adding response ld %p msgid %ld type %ld:\n",
999                 (void *)ld, (long) newmsg->lm_msgid, (long) newmsg->lm_msgtype );
1000
1001         /* part of a search response - add to end of list of entries */
1002         l->lm_chain_tail->lm_chain = newmsg;
1003         l->lm_chain_tail = newmsg;
1004
1005         /* return the whole chain if that's what we were looking for */
1006         if ( foundit ) {
1007                 if ( prev == NULL )
1008                         ld->ld_responses = l->lm_next;
1009                 else
1010                         prev->lm_next = l->lm_next;
1011                 *result = l;
1012         }
1013
1014 exit:
1015         if ( foundit ) {
1016                 ld->ld_errno = LDAP_SUCCESS;
1017                 return( tag );
1018         }
1019         if ( lc && ber_sockbuf_ctrl( lc->lconn_sb, LBER_SB_OPT_DATA_READY, NULL ) ) {
1020                 goto retry;
1021         }
1022         return( LDAP_MSG_X_KEEP_LOOKING );      /* continue looking */
1023 }
1024
1025
1026 static ber_tag_t
1027 build_result_ber( LDAP *ld, BerElement **bp, LDAPRequest *lr )
1028 {
1029         ber_len_t       len;
1030         ber_tag_t       tag;
1031         ber_int_t       along;
1032         BerElement *ber;
1033
1034         *bp = NULL;
1035         ber = ldap_alloc_ber_with_options( ld );
1036
1037         if( ber == NULL ) {
1038                 ld->ld_errno = LDAP_NO_MEMORY;
1039                 return LBER_ERROR;
1040         }
1041
1042         if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
1043                 lr->lr_res_msgtype, lr->lr_res_errno,
1044                 lr->lr_res_matched ? lr->lr_res_matched : "",
1045                 lr->lr_res_error ? lr->lr_res_error : "" ) == -1 )
1046         {
1047                 ld->ld_errno = LDAP_ENCODING_ERROR;
1048                 ber_free(ber, 1);
1049                 return( LBER_ERROR );
1050         }
1051
1052         ber_reset( ber, 1 );
1053
1054         if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) {
1055                 ld->ld_errno = LDAP_DECODING_ERROR;
1056                 ber_free(ber, 1);
1057                 return( LBER_ERROR );
1058         }
1059
1060         if ( ber_get_enum( ber, &along ) == LBER_ERROR ) {
1061                 ld->ld_errno = LDAP_DECODING_ERROR;
1062                 ber_free(ber, 1);
1063                 return( LBER_ERROR );
1064         }
1065
1066         tag = ber_peek_tag( ber, &len );
1067
1068         if ( tag == LBER_ERROR ) {
1069                 ld->ld_errno = LDAP_DECODING_ERROR;
1070                 ber_free(ber, 1);
1071                 return( LBER_ERROR );
1072         }
1073
1074         *bp = ber;
1075         return tag;
1076 }
1077
1078
1079 static void
1080 merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
1081 {
1082 /*
1083  * Merge error information in "lr" with "parentr" error code and string.
1084  */
1085         if ( lr->lr_res_errno == LDAP_PARTIAL_RESULTS ) {
1086                 parentr->lr_res_errno = lr->lr_res_errno;
1087                 if ( lr->lr_res_error != NULL ) {
1088                         (void)ldap_append_referral( ld, &parentr->lr_res_error,
1089                             lr->lr_res_error );
1090                 }
1091         } else if ( lr->lr_res_errno != LDAP_SUCCESS &&
1092                 parentr->lr_res_errno == LDAP_SUCCESS )
1093         {
1094                 parentr->lr_res_errno = lr->lr_res_errno;
1095                 if ( parentr->lr_res_error != NULL ) {
1096                         LDAP_FREE( parentr->lr_res_error );
1097                 }
1098                 parentr->lr_res_error = lr->lr_res_error;
1099                 lr->lr_res_error = NULL;
1100                 if ( LDAP_NAME_ERROR( lr->lr_res_errno ) ) {
1101                         if ( parentr->lr_res_matched != NULL ) {
1102                                 LDAP_FREE( parentr->lr_res_matched );
1103                         }
1104                         parentr->lr_res_matched = lr->lr_res_matched;
1105                         lr->lr_res_matched = NULL;
1106                 }
1107         }
1108
1109         Debug( LDAP_DEBUG_TRACE, "merged parent (id %d) error info:  ",
1110             parentr->lr_msgid, 0, 0 );
1111         Debug( LDAP_DEBUG_TRACE, "result errno %d, error <%s>, matched <%s>\n",
1112             parentr->lr_res_errno, parentr->lr_res_error ?
1113             parentr->lr_res_error : "", parentr->lr_res_matched ?
1114             parentr->lr_res_matched : "" );
1115 }
1116
1117
1118
1119 int
1120 ldap_msgtype( LDAPMessage *lm )
1121 {
1122         assert( lm != NULL );
1123         return ( lm != NULL ) ? (int)lm->lm_msgtype : -1;
1124 }
1125
1126
1127 int
1128 ldap_msgid( LDAPMessage *lm )
1129 {
1130         assert( lm != NULL );
1131
1132         return ( lm != NULL ) ? lm->lm_msgid : -1;
1133 }
1134
1135
1136 char * ldap_int_msgtype2str( ber_tag_t tag )
1137 {
1138         switch( tag ) {
1139         case LDAP_RES_ADD: return "add";
1140         case LDAP_RES_BIND: return "bind";
1141         case LDAP_RES_COMPARE: return "compare";
1142         case LDAP_RES_DELETE: return "delete";
1143         case LDAP_RES_EXTENDED: return "extended-result";
1144         case LDAP_RES_INTERMEDIATE: return "intermediate";
1145         case LDAP_RES_MODIFY: return "modify";
1146         case LDAP_RES_RENAME: return "rename";
1147         case LDAP_RES_SEARCH_ENTRY: return "search-entry";
1148         case LDAP_RES_SEARCH_REFERENCE: return "search-reference";
1149         case LDAP_RES_SEARCH_RESULT: return "search-result";
1150         }
1151         return "unknown";
1152 }
1153
1154 int
1155 ldap_msgfree( LDAPMessage *lm )
1156 {
1157         LDAPMessage     *next;
1158         int             type = 0;
1159
1160         Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
1161
1162         for ( ; lm != NULL; lm = next ) {
1163                 next = lm->lm_chain;
1164                 type = lm->lm_msgtype;
1165                 ber_free( lm->lm_ber, 1 );
1166                 LDAP_FREE( (char *) lm );
1167         }
1168
1169         return( type );
1170 }
1171
1172 /*
1173  * ldap_msgdelete - delete a message.  It returns:
1174  *      0       if the entire message was deleted
1175  *      -1      if the message was not found, or only part of it was found
1176  */
1177 int
1178 ldap_msgdelete( LDAP *ld, int msgid )
1179 {
1180         LDAPMessage     *lm, *prev;
1181         int rc = 0;
1182
1183         assert( ld != NULL );
1184
1185         Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete\n", 0, 0, 0 );
1186
1187         prev = NULL;
1188 #ifdef LDAP_R_COMPILE
1189         ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
1190 #endif
1191         for ( lm = ld->ld_responses; lm != NULL; lm = lm->lm_next ) {
1192                 if ( lm->lm_msgid == msgid )
1193                         break;
1194                 prev = lm;
1195         }
1196
1197         if ( lm == NULL ) {
1198                 rc = -1;
1199         } else {
1200                 if ( prev == NULL )
1201                         ld->ld_responses = lm->lm_next;
1202                 else
1203                         prev->lm_next = lm->lm_next;
1204         }
1205 #ifdef LDAP_R_COMPILE
1206         ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
1207 #endif
1208         if ( lm && ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY )
1209                 rc = -1;
1210
1211         return( rc );
1212 }
1213
1214
1215 /*
1216  * return 1 if message msgid is waiting to be abandoned, 0 otherwise
1217  */
1218 static int
1219 ldap_abandoned( LDAP *ld, ber_int_t msgid )
1220 {
1221         int     i;
1222
1223         if ( ld->ld_abandoned == NULL )
1224                 return( 0 );
1225
1226         for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
1227                 if ( ld->ld_abandoned[i] == msgid )
1228                         return( 1 );
1229
1230         return( 0 );
1231 }
1232
1233
1234 static int
1235 ldap_mark_abandoned( LDAP *ld, ber_int_t msgid )
1236 {
1237         int     i;
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 }