]> git.sur5r.net Git - openldap/blob - libraries/libldap/result.c
ITS#897 Internal connection that is closed on one end and about to
[openldap] / libraries / libldap / result.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*  Portions
7  *  Copyright (c) 1990 Regents of the University of Michigan.
8  *  All rights reserved.
9  */
10 /*---
11  * This notice applies to changes, created by or for Novell, Inc.,
12  * to preexisting works for which notices appear elsewhere in this file.
13  *
14  * Copyright (C) 1999, 2000 Novell, Inc. All Rights Reserved.
15  *
16  * THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND TREATIES.
17  * USE, MODIFICATION, AND REDISTRIBUTION OF THIS WORK IS SUBJECT TO VERSION
18  * 2.0.1 OF THE OPENLDAP PUBLIC LICENSE, A COPY OF WHICH IS AVAILABLE AT
19  * HTTP://WWW.OPENLDAP.ORG/LICENSE.HTML OR IN THE FILE "LICENSE" IN THE
20  * TOP-LEVEL DIRECTORY OF THE DISTRIBUTION. ANY USE OR EXPLOITATION OF THIS
21  * WORK OTHER THAN AS AUTHORIZED IN VERSION 2.0.1 OF THE OPENLDAP PUBLIC
22  * LICENSE, OR OTHER PRIOR WRITTEN CONSENT FROM NOVELL, COULD SUBJECT THE
23  * PERPETRATOR TO CRIMINAL AND CIVIL LIABILITY. 
24  *---
25  * Modification to OpenLDAP source by Novell, Inc.
26  * April 2000 sfs Add code to process V3 referrals and search results
27  *
28  *  result.c - wait for an ldap result
29  */
30
31 /*
32  * LDAPv3 (RFC2251)
33  *      LDAPResult ::= SEQUENCE {
34  *              resultCode              ENUMERATED { ... },
35  *              matchedDN               LDAPDN,
36  *              errorMessage    LDAPString,
37  *              referral                Referral OPTIONAL
38  *      }
39  *      Referral ::= SEQUENCE OF LDAPURL        (one or more)
40  *      LDAPURL ::= LDAPString                          (limited to URL chars)
41  */
42
43 #include "portable.h"
44
45 #include <stdio.h>
46
47 #include <ac/stdlib.h>
48
49 #include <ac/errno.h>
50 #include <ac/socket.h>
51 #include <ac/string.h>
52 #include <ac/time.h>
53 #include <ac/unistd.h>
54
55 #include "ldap-int.h"
56
57
58 static int ldap_abandoned LDAP_P(( LDAP *ld, ber_int_t msgid ));
59 static int ldap_mark_abandoned LDAP_P(( LDAP *ld, ber_int_t msgid ));
60 static int wait4msg LDAP_P(( LDAP *ld, ber_int_t msgid, int all, struct timeval *timeout,
61         LDAPMessage **result ));
62 static ber_tag_t try_read1msg LDAP_P(( LDAP *ld, ber_int_t msgid,
63         int all, Sockbuf *sb, LDAPConn *lc, LDAPMessage **result ));
64 static ber_tag_t build_result_ber LDAP_P(( LDAP *ld, BerElement **bp, LDAPRequest *lr ));
65 static void merge_error_info LDAP_P(( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr ));
66 static LDAPMessage * chkResponseList LDAP_P(( LDAP *ld, int msgid, int all));
67
68
69 /*
70  * ldap_result - wait for an ldap result response to a message from the
71  * ldap server.  If msgid is LDAP_RES_ANY (-1), any message will be
72  * accepted.  If msgid is LDAP_RES_UNSOLICITED (0), any unsolicited
73  * message is accepted.  Otherwise ldap_result will wait for a response
74  * with msgid.  If all is LDAP_MSG_ONE (0) the first message with id
75  * msgid will be accepted, otherwise, ldap_result will wait for all
76  * responses with id msgid and then return a pointer to the entire list
77  * of messages.  In general, this is only useful for search responses,
78  * which can be of three message types (zero or more entries, zero or
79  * search references, followed by an ldap result).  An extension to
80  * LDAPv3 allows partial extended responses to be returned in response
81  * to any request.  The type of the first message received is returned.
82  * When waiting, any messages that have been abandoned are discarded.
83  *
84  * Example:
85  *      ldap_result( s, msgid, all, timeout, result )
86  */
87 int
88 ldap_result(
89         LDAP *ld,
90         int msgid,
91         int all,
92         struct timeval *timeout,
93         LDAPMessage **result )
94 {
95         LDAPMessage     *lm;
96
97         assert( ld != NULL );
98         assert( result != NULL );
99
100         Debug( LDAP_DEBUG_TRACE, "ldap_result msgid %d\n", msgid, 0, 0 );
101
102         if( ld == NULL ) {
103                 return -1;
104         }
105
106         if( result == NULL ) {
107                 ld->ld_errno = LDAP_PARAM_ERROR;
108                 return -1;
109         }
110
111     lm = chkResponseList(ld, msgid, all);
112
113         if ( lm == NULL ) {
114                 return( wait4msg( ld, msgid, all, timeout, result ) );
115         }
116
117         *result = lm;
118         ld->ld_errno = LDAP_SUCCESS;
119         return( lm->lm_msgtype );
120 }
121
122 static LDAPMessage *
123 chkResponseList(
124         LDAP *ld,
125         int msgid,
126         int all)
127 {
128         LDAPMessage     *lm, *lastlm, *nextlm;
129     /*
130          * Look through the list of responses we have received on
131          * this association and see if the response we're interested in
132          * is there.  If it is, return it.  If not, call wait4msg() to
133          * wait until it arrives or timeout occurs.
134          */
135
136         Debug( LDAP_DEBUG_TRACE,
137                 "ldap_chkResponseList for msgid=%d, all=%d\n",
138             msgid, all, 0 );
139         lastlm = NULL;
140         for ( lm = ld->ld_responses; lm != NULL; lm = nextlm ) {
141                 nextlm = lm->lm_next;
142
143                 if ( ldap_abandoned( ld, lm->lm_msgid ) ) {
144                         Debug( LDAP_DEBUG_TRACE,
145                                 "ldap_chkResponseList msg abandoned, msgid %d\n",
146                             msgid, 0, 0 );
147                         ldap_mark_abandoned( ld, lm->lm_msgid );
148
149                         if ( lastlm == NULL ) {
150                                 /* Remove first entry in list */
151                                 ld->ld_responses = lm->lm_next;
152                         } else {
153                                 lastlm->lm_next = nextlm;
154                         }
155
156                         ldap_msgfree( lm );
157
158                         continue;
159                 }
160
161                 if ( msgid == LDAP_RES_ANY || lm->lm_msgid == msgid ) {
162                         LDAPMessage     *tmp;
163
164                         if ( all == LDAP_MSG_ONE || msgid == LDAP_RES_UNSOLICITED ) {
165                                 break;
166                         }
167
168                         for ( tmp = lm; tmp != NULL; tmp = tmp->lm_chain ) {
169                                 if ( lm->lm_msgtype != LDAP_RES_SEARCH_ENTRY
170                                     && lm->lm_msgtype != LDAP_RES_SEARCH_REFERENCE
171                                         && lm->lm_msgtype != LDAP_RES_EXTENDED_PARTIAL )
172                                 {
173                                         break;
174                                 }
175                         }
176
177                         if ( tmp == NULL ) {
178                                 lm = NULL;
179                         }
180
181                         break;
182                 }
183                 lastlm = lm;
184         }
185
186     if ( lm != NULL ) {
187                 /* Found an entry, remove it from the list */
188             if ( lastlm == NULL ) {
189                     ld->ld_responses = (all == LDAP_MSG_ONE && lm->lm_chain != NULL
190                         ? lm->lm_chain : lm->lm_next);
191             } else {
192                     lastlm->lm_next = (all == LDAP_MSG_ONE && lm->lm_chain != NULL
193                         ? lm->lm_chain : lm->lm_next);
194             }
195             if ( all == LDAP_MSG_ONE && lm->lm_chain != NULL ) {
196                     lm->lm_chain->lm_next = lm->lm_next;
197                     lm->lm_chain = NULL;
198             }
199             lm->lm_next = NULL;
200     }
201
202 #ifdef LDAP_DEBUG
203         if( lm == NULL) {
204                 Debug( LDAP_DEBUG_TRACE,
205                         "ldap_chkResponseList returns NULL\n", 0, 0, 0);
206         } else {
207                 Debug( LDAP_DEBUG_TRACE,
208                         "ldap_chkResponseList returns msgid %d, type 0x%02lu\n",
209                         lm->lm_msgid, (unsigned long) lm->lm_msgtype, 0);
210         }
211 #endif
212     return lm;
213 }
214 static int
215 wait4msg(
216         LDAP *ld,
217         ber_int_t msgid,
218         int all,
219         struct timeval *timeout,
220         LDAPMessage **result )
221 {
222         int             rc;
223         struct timeval  tv, *tvp;
224         time_t          start_time = 0;
225         time_t          tmp_time;
226         LDAPConn        *lc, *nextlc;
227
228         assert( ld != NULL );
229         assert( result != NULL );
230
231 #ifdef LDAP_DEBUG
232         if ( timeout == NULL ) {
233                 Debug( LDAP_DEBUG_TRACE, "wait4msg (infinite timeout), msgid %d\n",
234                     msgid, 0, 0 );
235         } else {
236                 Debug( LDAP_DEBUG_TRACE, "wait4msg (timeout %ld sec, %ld usec), msgid %d\n",
237                        (long) timeout->tv_sec, (long) timeout->tv_usec, msgid );
238         }
239 #endif /* LDAP_DEBUG */
240
241         if ( timeout == NULL ) {
242                 tvp = NULL;
243         } else {
244                 tv = *timeout;
245                 tvp = &tv;
246                 start_time = time( NULL );
247         }
248                     
249         rc = -2;
250         while ( rc == -2 ) {
251 #ifdef LDAP_DEBUG
252                 Debug( LDAP_DEBUG_TRACE, "wait4msg continue, msgid %d, all %d\n",
253                     msgid, all, 0 );
254                 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
255                         ldap_dump_connection( ld, ld->ld_conns, 1 );
256                         ldap_dump_requests_and_responses( ld );
257                 }
258 #endif /* LDAP_DEBUG */
259
260         if( (*result = chkResponseList(ld, msgid, all)) != NULL ) {
261             rc = (*result)->lm_msgtype;
262         } else {
263
264                         for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
265                                 if ( ber_sockbuf_ctrl( lc->lconn_sb,
266                                                 LBER_SB_OPT_DATA_READY, NULL ) ) {
267                                             rc = try_read1msg( ld, msgid, all, lc->lconn_sb,
268                                                 lc, result );
269                                     break;
270                                 }
271                 }
272
273                     if ( lc == NULL ) {
274                             rc = do_ldap_select( ld, tvp );
275
276
277 #ifdef LDAP_DEBUG
278                             if ( rc == -1 ) {
279                                 Debug( LDAP_DEBUG_TRACE,
280                                         "do_ldap_select returned -1: errno %d\n",
281                                         errno, 0, 0 );
282                             }
283 #endif
284
285                             if ( rc == 0 || ( rc == -1 && (
286                                     !LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART)
287                                     || errno != EINTR )))
288                             {
289                                     ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
290                                         LDAP_TIMEOUT);
291                                     return( rc );
292                             }
293
294                             if ( rc == -1 ) {
295                                     rc = -2;    /* select interrupted: loop */
296                             } else {
297                                     rc = -2;
298                                     for ( lc = ld->ld_conns; rc == -2 && lc != NULL;
299                                         lc = nextlc ) {
300                                             nextlc = lc->lconn_next;
301                                             if ( lc->lconn_status ==
302                                                 LDAP_CONNST_CONNECTED &&
303                                                 ldap_is_read_ready( ld,
304                                                 lc->lconn_sb )) {
305                                                     rc = try_read1msg( ld, msgid, all,
306                                                         lc->lconn_sb, lc, result );
307                                             }
308                                     }
309                             }
310                     }
311                 }
312
313                 if ( rc == -2 && tvp != NULL ) {
314                         tmp_time = time( NULL );
315                         if (( tv.tv_sec -=  ( tmp_time - start_time )) <= 0 ) {
316                                 rc = 0; /* timed out */
317                                 ld->ld_errno = LDAP_TIMEOUT;
318                                 break;
319                         }
320
321                         Debug( LDAP_DEBUG_TRACE, "wait4msg:  %ld secs to go\n",
322                                (long) tv.tv_sec, 0, 0 );
323                         start_time = tmp_time;
324                 }
325         }
326
327         return( rc );
328 }
329
330
331 static ber_tag_t
332 try_read1msg(
333         LDAP *ld,
334         ber_int_t msgid,
335         int all,
336         Sockbuf *sb,
337     LDAPConn *lc,
338         LDAPMessage **result )
339 {
340         BerElement      *ber;
341         LDAPMessage     *new, *l, *prev, *tmp;
342         ber_int_t       id;
343         ber_tag_t       tag;
344         ber_len_t       len;
345         int             foundit = 0;
346         LDAPRequest     *lr, *tmplr;
347         BerElement      tmpber;
348         int             rc, refer_cnt, hadref, simple_request;
349         ber_int_t       lderr;
350         /*
351          * v3ref = flag for V3 referral / search reference
352          * 0 = not a ref, 1 = sucessfully chased ref, -1 = pass ref to application
353          */
354         int     v3ref;
355
356         assert( ld != NULL );
357         assert( lc != NULL );
358         
359         Debug( LDAP_DEBUG_TRACE, "read1msg: msgid %d, all %d\n", msgid, all, 0 );
360
361     if ( lc->lconn_ber == NULL ) {
362                 lc->lconn_ber = ldap_alloc_ber_with_options(ld);
363
364                 if( lc->lconn_ber == NULL ) {
365                         return -1;
366                 }
367     }
368
369         ber = lc->lconn_ber;
370         assert( BER_VALID (ber) );
371
372         /* get the next message */
373         errno = 0;
374         if ( (tag = ber_get_next( sb, &len, ber ))
375             != LDAP_TAG_MESSAGE ) {
376                 if ( tag == LBER_DEFAULT) {
377 #ifdef LDAP_DEBUG                  
378                         Debug( LDAP_DEBUG_CONNS,
379                               "ber_get_next failed.\n", 0, 0, 0 );
380 #endif             
381 #ifdef EWOULDBLOCK                      
382                         if (errno==EWOULDBLOCK) return -2;
383 #endif
384 #ifdef EAGAIN
385                         if (errno == EAGAIN) return -2;
386 #endif
387                         ld->ld_errno = LDAP_SERVER_DOWN;
388                         return -1;
389                 }
390                 ld->ld_errno = LDAP_LOCAL_ERROR;
391                 return -1;
392         }
393
394         /*
395      * We read a complete message.
396          * The connection should no longer need this ber.
397          */
398     lc->lconn_ber = NULL;
399
400         /* message id */
401         if ( ber_get_int( ber, &id ) == LBER_ERROR ) {
402                 ber_free( ber, 1 );
403                 ld->ld_errno = LDAP_DECODING_ERROR;
404                 return( -1 );
405         }
406
407         /* if it's been abandoned, toss it */
408         if ( ldap_abandoned( ld, id ) ) {
409                 ber_free( ber, 1 );
410                 Debug( LDAP_DEBUG_ANY, "abandoned\n", 0, 0, 0);
411                 return( -2 );   /* continue looking */
412         }
413
414         if (( lr = ldap_find_request_by_msgid( ld, id )) == NULL ) {
415                 Debug( LDAP_DEBUG_ANY,
416                     "no request for response with msgid %ld (tossing)\n",
417                     (long) id, 0, 0 );
418                 ber_free( ber, 1 );
419                 return( -2 );   /* continue looking */
420         }
421
422         /* the message type */
423         if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
424                 ld->ld_errno = LDAP_DECODING_ERROR;
425                 ber_free( ber, 1 );
426                 return( -1 );
427         }
428
429         Debug( LDAP_DEBUG_TRACE, "ldap_read: message type %s msgid %ld, original id %ld\n",
430             ldap_int_msgtype2str( tag ),
431                 (long) lr->lr_msgid, (long) lr->lr_origid );
432
433         id = lr->lr_origid;
434         refer_cnt = 0;
435         hadref = simple_request = 0;
436         rc = -2;        /* default is to keep looking (no response found) */
437         lr->lr_res_msgtype = tag;
438
439         /*
440          * This code figures out if we are going to chase a
441          * referral / search reference, or pass it back to the application
442          */
443         v3ref = 0;      /* Assume not a V3 search reference or referral */
444         if( (tag != LDAP_RES_SEARCH_ENTRY) && (ld->ld_version > LDAP_VERSION2) ) {
445                 BerElement      tmpber = *ber;  /* struct copy */
446                 char **refs = NULL;
447
448                 if( tag == LDAP_RES_SEARCH_REFERENCE) {
449                         /* This is a V3 search reference */
450                         /* Assume we do not chase the reference, but pass it to application */
451                         v3ref = -1;
452                         if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ||
453                                         (lr->lr_parent != NULL) )
454                         {
455                                 /* Get the referral list */
456                                 if ( ber_scanf( &tmpber, "{v}", &refs ) == LBER_ERROR ) {
457                                         rc = LDAP_DECODING_ERROR;
458                                 } else {
459                                         /* Note: refs arrary is freed by ldap_chase_v3referrals */
460                                         refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
461                                             1, &lr->lr_res_error, &hadref );
462                                         if ( refer_cnt > 0 ) {  /* sucessfully chased reference */
463                                                 /* If haven't got end search, set chasing referrals */
464                                                 if( lr->lr_status != LDAP_REQST_COMPLETED) {
465                                                         lr->lr_status = LDAP_REQST_CHASINGREFS;
466                                                         Debug( LDAP_DEBUG_TRACE,
467                                                             "read1msg:  search ref chased, mark request chasing refs, id = %d\n",
468                                                             lr->lr_msgid, 0, 0);
469                                                 }
470                                                 v3ref = 1;      /* We sucessfully chased the reference */
471                                         }
472                                 }
473                         }
474                 } else {
475                         /* Check for V3 referral */
476                         ber_len_t len;
477                         if ( ber_scanf( &tmpber, "{iaa",/*}*/ &lderr,
478                                     &lr->lr_res_matched, &lr->lr_res_error )
479                                     != LBER_ERROR ) {
480                                 /* Check if V3 referral */
481                                 if( ber_peek_tag( &tmpber, &len) == LDAP_TAG_REFERRAL ) {
482                                         /* We have a V3 referral, assume we cannot chase it */
483                                         v3ref = -1;
484                                         if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS)
485                                                          || (lr->lr_parent != NULL) )
486                                         {
487                                                 v3ref = -1;  /* Assume referral not chased and return it to app */
488                                                 /* Get the referral list */
489                                                 if( ber_scanf( &tmpber, "{v}", &refs) == LBER_ERROR) {
490                                                         rc = LDAP_DECODING_ERROR;
491                                                         lr->lr_status = LDAP_REQST_COMPLETED;
492                                                         Debug( LDAP_DEBUG_TRACE,
493                                                             "read1msg: referral decode error, mark request completed, id = %d\n",
494                                                                     lr->lr_msgid, 0, 0);
495                                                 } else {
496                                                         /* Chase the referral 
497                                                          * Note: refs arrary is freed by ldap_chase_v3referrals
498                                                          */
499                                                         refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
500                                                             0, &lr->lr_res_error, &hadref );
501                                                         lr->lr_status = LDAP_REQST_COMPLETED;
502                                                         Debug( LDAP_DEBUG_TRACE,
503                                                             "read1msg:  referral chased, mark request completed, id = %d\n",
504                                                             lr->lr_msgid, 0, 0);
505                                                         if( refer_cnt > 0) {
506                                                                 v3ref = 1;  /* Referral successfully chased */
507                                                         }
508                                                 }
509                                         }
510                                 }
511
512                                 if( lr->lr_res_matched != NULL ) {
513                                         LDAP_FREE( lr->lr_res_matched );
514                                         lr->lr_res_matched = NULL;
515                                 }
516                                 if( lr->lr_res_error != NULL ) {
517                                         LDAP_FREE( lr->lr_res_error );
518                                         lr->lr_res_error = NULL;
519                                 }
520                         }
521                 }
522         }
523
524         /* All results that just return a status, i.e. don't return data
525          * go through the following code.  This code also chases V2 referrals
526          * and checks if all referrals have been chased.
527          */
528         if ( (tag != LDAP_RES_SEARCH_ENTRY) && (v3ref > -1) ) {
529                 /* For a v3 search referral/reference, only come here if already chased it */
530                 if ( ld->ld_version >= LDAP_VERSION2 &&
531                         ( lr->lr_parent != NULL ||
532                         LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ) )
533                 {
534                         tmpber = *ber;  /* struct copy */
535                         if ( v3ref == 1 ) {
536                                 ; /* V3 search reference or V3 referral sucessfully chased */
537                         } else
538                         if ( ber_scanf( &tmpber, "{iaa}", &lderr,
539                             &lr->lr_res_matched, &lr->lr_res_error )
540                             != LBER_ERROR ) {
541                                 if ( lderr != LDAP_SUCCESS ) {
542                                         /* referrals are in error string */
543                                         refer_cnt = ldap_chase_referrals( ld, lr,
544                                             &lr->lr_res_error, &hadref );
545                                         lr->lr_status = LDAP_REQST_COMPLETED;
546                                         Debug( LDAP_DEBUG_TRACE,
547                                             "read1msg:  V2 referral chased, mark request completed, id = %d\n", lr->lr_msgid, 0, 0);
548                                 }
549
550                                 /* save errno, message, and matched string */
551                                 if ( !hadref || lr->lr_res_error == NULL ) {
552                                         lr->lr_res_errno = ( lderr ==
553                                         LDAP_PARTIAL_RESULTS ) ? LDAP_SUCCESS
554                                         : lderr;
555                                 } else if ( ld->ld_errno != LDAP_SUCCESS ) {
556                                         lr->lr_res_errno = ld->ld_errno;
557                                 } else {
558                                         lr->lr_res_errno = LDAP_PARTIAL_RESULTS;
559                                 }
560 Debug( LDAP_DEBUG_TRACE,
561     "new result:  res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
562     lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
563     lr->lr_res_matched ? lr->lr_res_matched : "" );
564                         }
565                 }
566
567                 Debug( LDAP_DEBUG_TRACE,
568                     "read1msg:  %d new referrals\n", refer_cnt, 0, 0 );
569
570                 if ( refer_cnt != 0 ) { /* chasing referrals */
571                         ber_free( ber, 1 );
572                         ber = NULL;
573                         if ( refer_cnt < 0 ) {
574                                 return( -1 );   /* fatal error */
575                         }
576                         lr->lr_res_errno = LDAP_SUCCESS; /* sucessfully chased referral */
577                 } else {
578                         if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
579                                 /* request without any referrals */
580                                 simple_request = ( hadref ? 0 : 1 );
581                         } else {
582                                 /* request with referrals or child request */
583                                 ber_free( ber, 1 );
584                                 ber = NULL;
585                         }
586
587                         lr->lr_status = LDAP_REQST_COMPLETED; /* declare this request done */
588                         Debug( LDAP_DEBUG_TRACE,
589                             "read1msg:  mark request completed, id = %d\n", lr->lr_msgid, 0, 0);
590                         while ( lr->lr_parent != NULL ) {
591                                 merge_error_info( ld, lr->lr_parent, lr );
592
593                                 lr = lr->lr_parent;
594                                 if ( --lr->lr_outrefcnt > 0 ) {
595                                         break;  /* not completely done yet */
596                                 }
597                         }
598
599                         /* Check if all requests are finished, lr is now parent */
600                         for(tmplr=lr ; tmplr != NULL; tmplr=tmplr->lr_refnext) {
601                                 if( tmplr->lr_status != LDAP_REQST_COMPLETED) {
602                                         break;
603                                 }
604                         }
605
606                         /* This is the parent request if the request has referrals */
607                         if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL && tmplr == NULL ) {
608                                 id = lr->lr_msgid;
609                                 tag = lr->lr_res_msgtype;
610                                 Debug( LDAP_DEBUG_ANY, "request %ld done\n",
611                                     (long) id, 0, 0 );
612 Debug( LDAP_DEBUG_TRACE,
613 "res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
614 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
615 lr->lr_res_matched ? lr->lr_res_matched : "" );
616                                 if ( !simple_request ) {
617                                         ber_free( ber, 1 );
618                                         ber = NULL;
619                                         if ( build_result_ber( ld, &ber, lr )
620                                             == LBER_ERROR ) {
621                                                 rc = -1; /* fatal error */
622                                         }
623                                 }
624
625                                 ldap_free_request( ld, lr );
626                         }
627
628                         if ( lc != NULL ) {
629                                 ldap_free_connection( ld, lc, 0, 1 );
630                         }
631                 }
632         }
633
634         if ( ber == NULL ) {
635                 return( rc );
636         }
637
638         /* make a new ldap message */
639         if ( (new = (LDAPMessage *) LDAP_CALLOC( 1, sizeof(LDAPMessage) ))
640             == NULL ) {
641                 ld->ld_errno = LDAP_NO_MEMORY;
642                 return( -1 );
643         }
644         new->lm_msgid = (int)id;
645         new->lm_msgtype = tag;
646         new->lm_ber = ber;
647
648 #ifndef LDAP_NOCACHE
649                 if ( ld->ld_cache != NULL ) {
650                         ldap_add_result_to_cache( ld, new );
651                 }
652 #endif /* LDAP_NOCACHE */
653
654         /* is this the one we're looking for? */
655         if ( msgid == LDAP_RES_ANY || id == msgid ) {
656                 if ( all == LDAP_MSG_ONE
657                     || (new->lm_msgtype != LDAP_RES_SEARCH_RESULT
658                     && new->lm_msgtype != LDAP_RES_SEARCH_ENTRY
659                     && new->lm_msgtype != LDAP_RES_SEARCH_REFERENCE) ) {
660                         *result = new;
661                         ld->ld_errno = LDAP_SUCCESS;
662                         return( tag );
663                 } else if ( new->lm_msgtype == LDAP_RES_SEARCH_RESULT) {
664                         foundit = 1;    /* return the chain later */
665                 }
666         }
667
668         /* 
669          * if not, we must add it to the list of responses.  if
670          * the msgid is already there, it must be part of an existing
671          * search response.
672          */
673
674         prev = NULL;
675         for ( l = ld->ld_responses; l != NULL; l = l->lm_next ) {
676                 if ( l->lm_msgid == new->lm_msgid )
677                         break;
678                 prev = l;
679         }
680
681         /* not part of an existing search response */
682         if ( l == NULL ) {
683                 if ( foundit ) {
684                         *result = new;
685                         ld->ld_errno = LDAP_SUCCESS;
686                         return( tag );
687                 }
688
689                 new->lm_next = ld->ld_responses;
690                 ld->ld_responses = new;
691                 return( -2 );   /* continue looking */
692         }
693
694         Debug( LDAP_DEBUG_TRACE, "adding response id %ld type %ld:\n",
695             (long) new->lm_msgid, (long) new->lm_msgtype, 0 );
696
697         /* part of a search response - add to end of list of entries */
698         for ( tmp = l; (tmp->lm_chain != NULL) &&
699                 ((tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY) ||
700                  (tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_REFERENCE) ||
701                          (tmp->lm_chain->lm_msgtype == LDAP_RES_EXTENDED_PARTIAL ));
702             tmp = tmp->lm_chain )
703                 ;       /* NULL */
704         tmp->lm_chain = new;
705
706         /* return the whole chain if that's what we were looking for */
707         if ( foundit ) {
708                 if ( prev == NULL )
709                         ld->ld_responses = l->lm_next;
710                 else
711                         prev->lm_next = l->lm_next;
712                 *result = l;
713                 ld->ld_errno = LDAP_SUCCESS;
714 #ifdef LDAP_WORLD_P16
715                 /*
716                  * XXX questionable fix; see text for [P16] on
717                  * http://www.critical-angle.com/ldapworld/patch/
718                  *
719                  * inclusion of this patch causes searchs to hang on
720                  * multiple platforms
721                  */
722                 return( l->lm_msgtype );
723 #else   /* LDAP_WORLD_P16 */
724                 return( tag );
725 #endif  /* !LDAP_WORLD_P16 */
726         }
727
728         return( -2 );   /* continue looking */
729 }
730
731
732 static ber_tag_t
733 build_result_ber( LDAP *ld, BerElement **bp, LDAPRequest *lr )
734 {
735         ber_len_t       len;
736         ber_int_t       tag;
737         ber_int_t       along;
738         BerElement *ber;
739
740         *bp = NULL;
741         ber = ldap_alloc_ber_with_options( ld );
742
743         if( ber == NULL ) {
744                 ld->ld_errno = LDAP_NO_MEMORY;
745                 return LBER_ERROR;
746         }
747
748         if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
749             lr->lr_res_msgtype, lr->lr_res_errno,
750             lr->lr_res_matched ? lr->lr_res_matched : "",
751             lr->lr_res_error ? lr->lr_res_error : "" ) == -1 ) {
752
753                 ld->ld_errno = LDAP_ENCODING_ERROR;
754                 ber_free(ber, 1);
755                 return( LBER_ERROR );
756         }
757
758         ber_reset( ber, 1 );
759
760         if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) {
761                 ld->ld_errno = LDAP_DECODING_ERROR;
762                 ber_free(ber, 1);
763                 return( LBER_ERROR );
764         }
765
766         if ( ber_get_int( ber, &along ) == LBER_ERROR ) {
767                 ld->ld_errno = LDAP_DECODING_ERROR;
768                 ber_free(ber, 1);
769                 return( LBER_ERROR );
770         }
771
772         tag = ber_peek_tag( ber, &len );
773
774         if ( tag == LBER_ERROR ) {
775                 ld->ld_errno = LDAP_DECODING_ERROR;
776                 ber_free(ber, 1);
777                 return( LBER_ERROR );
778         }
779
780         *bp = ber;
781         return tag;
782 }
783
784
785 static void
786 merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
787 {
788 /*
789  * Merge error information in "lr" with "parentr" error code and string.
790  */
791         if ( lr->lr_res_errno == LDAP_PARTIAL_RESULTS ) {
792                 parentr->lr_res_errno = lr->lr_res_errno;
793                 if ( lr->lr_res_error != NULL ) {
794                         (void)ldap_append_referral( ld, &parentr->lr_res_error,
795                             lr->lr_res_error );
796                 }
797         } else if ( lr->lr_res_errno != LDAP_SUCCESS &&
798             parentr->lr_res_errno == LDAP_SUCCESS ) {
799                 parentr->lr_res_errno = lr->lr_res_errno;
800                 if ( parentr->lr_res_error != NULL ) {
801                         LDAP_FREE( parentr->lr_res_error );
802                 }
803                 parentr->lr_res_error = lr->lr_res_error;
804                 lr->lr_res_error = NULL;
805                 if ( LDAP_NAME_ERROR( lr->lr_res_errno )) {
806                         if ( parentr->lr_res_matched != NULL ) {
807                                 LDAP_FREE( parentr->lr_res_matched );
808                         }
809                         parentr->lr_res_matched = lr->lr_res_matched;
810                         lr->lr_res_matched = NULL;
811                 }
812         }
813
814         Debug( LDAP_DEBUG_TRACE, "merged parent (id %d) error info:  ",
815             parentr->lr_msgid, 0, 0 );
816         Debug( LDAP_DEBUG_TRACE, "result errno %d, error <%s>, matched <%s>\n",
817             parentr->lr_res_errno, parentr->lr_res_error ?
818             parentr->lr_res_error : "", parentr->lr_res_matched ?
819             parentr->lr_res_matched : "" );
820 }
821
822
823
824 int
825 ldap_msgtype( LDAPMessage *lm )
826 {
827         assert( lm != NULL );
828         return ( lm != NULL ) ? lm->lm_msgtype : -1;
829 }
830
831
832 int
833 ldap_msgid( LDAPMessage *lm )
834 {
835         assert( lm != NULL );
836
837         return ( lm != NULL ) ? lm->lm_msgid : -1;
838 }
839
840
841 char * ldap_int_msgtype2str( ber_tag_t tag )
842 {
843         switch( tag ) {
844         case LDAP_RES_ADD: return "add";
845         case LDAP_RES_BIND: return "bind";
846         case LDAP_RES_COMPARE: return "compare";
847         case LDAP_RES_DELETE: return "delete";
848         case LDAP_RES_EXTENDED: return "extended-result";
849         case LDAP_RES_EXTENDED_PARTIAL: return "extended-partial";
850         case LDAP_RES_MODIFY: return "modify";
851         case LDAP_RES_RENAME: return "rename";
852         case LDAP_RES_SEARCH_ENTRY: return "search-entry";
853         case LDAP_RES_SEARCH_REFERENCE: return "search-reference";
854         case LDAP_RES_SEARCH_RESULT: return "search-result";
855         }
856         return "unknown";
857 }
858
859 int
860 ldap_msgfree( LDAPMessage *lm )
861 {
862         LDAPMessage     *next;
863         int             type = 0;
864
865         Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
866
867         for ( ; lm != NULL; lm = next ) {
868                 next = lm->lm_chain;
869                 type = lm->lm_msgtype;
870                 ber_free( lm->lm_ber, 1 );
871                 LDAP_FREE( (char *) lm );
872         }
873
874         return( type );
875 }
876
877 /*
878  * ldap_msgdelete - delete a message.  It returns:
879  *      0       if the entire message was deleted
880  *      -1      if the message was not found, or only part of it was found
881  */
882 int
883 ldap_msgdelete( LDAP *ld, int msgid )
884 {
885         LDAPMessage     *lm, *prev;
886
887         assert( ld != NULL );
888
889         Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete\n", 0, 0, 0 );
890
891         prev = NULL;
892         for ( lm = ld->ld_responses; lm != NULL; lm = lm->lm_next ) {
893                 if ( lm->lm_msgid == msgid )
894                         break;
895                 prev = lm;
896         }
897
898         if ( lm == NULL )
899                 return( -1 );
900
901         if ( prev == NULL )
902                 ld->ld_responses = lm->lm_next;
903         else
904                 prev->lm_next = lm->lm_next;
905
906         if ( ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY )
907                 return( -1 );
908
909         return( 0 );
910 }
911
912
913 /*
914  * return 1 if message msgid is waiting to be abandoned, 0 otherwise
915  */
916 static int
917 ldap_abandoned( LDAP *ld, ber_int_t msgid )
918 {
919         int     i;
920
921         if ( ld->ld_abandoned == NULL )
922                 return( 0 );
923
924         for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
925                 if ( ld->ld_abandoned[i] == msgid )
926                         return( 1 );
927
928         return( 0 );
929 }
930
931
932 static int
933 ldap_mark_abandoned( LDAP *ld, ber_int_t msgid )
934 {
935         int     i;
936
937         if ( ld->ld_abandoned == NULL )
938                 return( -1 );
939
940         for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
941                 if ( ld->ld_abandoned[i] == msgid )
942                         break;
943
944         if ( ld->ld_abandoned[i] == -1 )
945                 return( -1 );
946
947         for ( ; ld->ld_abandoned[i] != -1; i++ ) {
948                 ld->ld_abandoned[i] = ld->ld_abandoned[i + 1];
949         }
950
951         return( 0 );
952 }