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