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