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