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