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