]> git.sur5r.net Git - openldap/blob - libraries/libldap/result.c
918bba0029c8aa397ccdb6203c9b249b10659217
[openldap] / libraries / libldap / result.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 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 ( tmp->lm_msgtype != LDAP_RES_SEARCH_ENTRY
170                                     && tmp->lm_msgtype != LDAP_RES_SEARCH_REFERENCE
171                                         && tmp->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
215 static int
216 wait4msg(
217         LDAP *ld,
218         ber_int_t msgid,
219         int all,
220         struct timeval *timeout,
221         LDAPMessage **result )
222 {
223         int             rc;
224         struct timeval  tv, *tvp;
225         time_t          start_time = 0;
226         time_t          tmp_time;
227         LDAPConn        *lc, *nextlc;
228
229         assert( ld != NULL );
230         assert( result != NULL );
231
232 #ifdef LDAP_DEBUG
233         if ( timeout == NULL ) {
234                 Debug( LDAP_DEBUG_TRACE, "wait4msg (infinite timeout), msgid %d\n",
235                     msgid, 0, 0 );
236         } else {
237                 Debug( LDAP_DEBUG_TRACE, "wait4msg (timeout %ld sec, %ld usec), msgid %d\n",
238                        (long) timeout->tv_sec, (long) timeout->tv_usec, msgid );
239         }
240 #endif /* LDAP_DEBUG */
241
242         if ( timeout == NULL ) {
243                 tvp = NULL;
244         } else {
245                 tv = *timeout;
246                 tvp = &tv;
247                 start_time = time( NULL );
248         }
249                     
250         rc = -2;
251         while ( rc == -2 ) {
252 #ifdef LDAP_DEBUG
253                 Debug( LDAP_DEBUG_TRACE, "wait4msg continue, msgid %d, all %d\n",
254                     msgid, all, 0 );
255                 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
256                         ldap_dump_connection( ld, ld->ld_conns, 1 );
257                         ldap_dump_requests_and_responses( ld );
258                 }
259 #endif /* LDAP_DEBUG */
260
261         if( (*result = chkResponseList(ld, msgid, all)) != NULL ) {
262             rc = (*result)->lm_msgtype;
263         } else {
264
265                         for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
266                                 if ( ber_sockbuf_ctrl( lc->lconn_sb,
267                                                 LBER_SB_OPT_DATA_READY, NULL ) ) {
268                                             rc = try_read1msg( ld, msgid, all, lc->lconn_sb,
269                                                 lc, result );
270                                     break;
271                                 }
272                 }
273
274                     if ( lc == NULL ) {
275                             rc = ldap_int_select( ld, tvp );
276
277
278 #ifdef LDAP_DEBUG
279                             if ( rc == -1 ) {
280                                 Debug( LDAP_DEBUG_TRACE,
281                                         "ldap_int_select returned -1: errno %d\n",
282                                         errno, 0, 0 );
283                             }
284 #endif
285
286                             if ( rc == 0 || ( rc == -1 && (
287                                     !LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART)
288                                     || errno != EINTR )))
289                             {
290                                     ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
291                                         LDAP_TIMEOUT);
292                                     return( rc );
293                             }
294
295                             if ( rc == -1 ) {
296                                     rc = -2;    /* select interrupted: loop */
297                             } else {
298                                     rc = -2;
299                                     for ( lc = ld->ld_conns; rc == -2 && lc != NULL;
300                                         lc = nextlc ) {
301                                             nextlc = lc->lconn_next;
302                                             if ( lc->lconn_status ==
303                                                 LDAP_CONNST_CONNECTED &&
304                                                 ldap_is_read_ready( ld,
305                                                 lc->lconn_sb )) {
306                                                     rc = try_read1msg( ld, msgid, all,
307                                                         lc->lconn_sb, lc, result );
308                                             }
309                                     }
310                             }
311                     }
312                 }
313
314                 if ( rc == -2 && tvp != NULL ) {
315                         tmp_time = time( NULL );
316                         if (( tv.tv_sec -=  ( tmp_time - start_time )) <= 0 ) {
317                                 rc = 0; /* timed out */
318                                 ld->ld_errno = LDAP_TIMEOUT;
319                                 break;
320                         }
321
322                         Debug( LDAP_DEBUG_TRACE, "wait4msg:  %ld secs to go\n",
323                                (long) tv.tv_sec, 0, 0 );
324                         start_time = tmp_time;
325                 }
326         }
327
328         return( rc );
329 }
330
331
332 static ber_tag_t
333 try_read1msg(
334         LDAP *ld,
335         ber_int_t msgid,
336         int all,
337         Sockbuf *sb,
338         LDAPConn *lc,
339         LDAPMessage **result )
340 {
341         BerElement      *ber;
342         LDAPMessage     *new, *l, *prev, *tmp;
343         ber_int_t       id;
344         ber_tag_t       tag;
345         ber_len_t       len;
346         int             foundit = 0;
347         LDAPRequest     *lr, *tmplr;
348         BerElement      tmpber;
349         int             rc, refer_cnt, hadref, simple_request;
350         ber_int_t       lderr;
351         /*
352          * v3ref = flag for V3 referral / search reference
353          * 0 = not a ref, 1 = sucessfully chased ref, -1 = pass ref to application
354          */
355         int     v3ref;
356
357         assert( ld != NULL );
358         assert( lc != NULL );
359         
360         Debug( LDAP_DEBUG_TRACE, "read1msg: msgid %d, all %d\n", msgid, all, 0 );
361
362     if ( lc->lconn_ber == NULL ) {
363                 lc->lconn_ber = ldap_alloc_ber_with_options(ld);
364
365                 if( lc->lconn_ber == NULL ) {
366                         return -1;
367                 }
368     }
369
370         ber = lc->lconn_ber;
371         assert( LBER_VALID (ber) );
372
373         /* get the next message */
374         errno = 0;
375 #ifdef LDAP_CONNECTIONLESS
376         if ( LDAP_IS_UDP(ld) ) {
377                 struct sockaddr from;
378                 ber_int_sb_read(sb, &from, sizeof(struct sockaddr));
379         }
380 #endif
381         if ( (tag = ber_get_next( sb, &len, ber ))
382             != LDAP_TAG_MESSAGE ) {
383                 if ( tag == LBER_DEFAULT) {
384 #ifdef LDAP_DEBUG                  
385                         Debug( LDAP_DEBUG_CONNS,
386                               "ber_get_next failed.\n", 0, 0, 0 );
387 #endif             
388 #ifdef EWOULDBLOCK                      
389                         if (errno==EWOULDBLOCK) return -2;
390 #endif
391 #ifdef EAGAIN
392                         if (errno == EAGAIN) return -2;
393 #endif
394                         ld->ld_errno = LDAP_SERVER_DOWN;
395                         return -1;
396                 }
397                 ld->ld_errno = LDAP_LOCAL_ERROR;
398                 return -1;
399         }
400
401         /*
402      * We read a complete message.
403          * The connection should no longer need this ber.
404          */
405     lc->lconn_ber = NULL;
406
407         /* message id */
408         if ( ber_get_int( ber, &id ) == LBER_ERROR ) {
409                 ber_free( ber, 1 );
410                 ld->ld_errno = LDAP_DECODING_ERROR;
411                 return( -1 );
412         }
413
414         /* if it's been abandoned, toss it */
415         if ( ldap_abandoned( ld, id ) ) {
416                 ber_free( ber, 1 );
417                 Debug( LDAP_DEBUG_ANY, "abandoned\n", 0, 0, 0);
418                 return( -2 );   /* continue looking */
419         }
420
421         if (( lr = ldap_find_request_by_msgid( ld, id )) == NULL ) {
422                 Debug( LDAP_DEBUG_ANY,
423                     "no request for response with msgid %ld (tossing)\n",
424                     (long) id, 0, 0 );
425                 ber_free( ber, 1 );
426                 return( -2 );   /* continue looking */
427         }
428 #ifdef LDAP_CONNECTIONLESS
429         if (LDAP_IS_UDP(ld) && ld->ld_options.ldo_version == LDAP_VERSION2) {
430                 char *blank = NULL;
431                 ber_scanf(ber, "a{", &blank);
432                 if (blank) ber_memfree(blank);
433         }
434 #endif
435         /* the message type */
436         if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
437                 ld->ld_errno = LDAP_DECODING_ERROR;
438                 ber_free( ber, 1 );
439                 return( -1 );
440         }
441
442         Debug( LDAP_DEBUG_TRACE,
443                 "ldap_read: message type %s msgid %ld, original id %ld\n",
444             ldap_int_msgtype2str( tag ),
445                 (long) lr->lr_msgid, (long) lr->lr_origid );
446
447         id = lr->lr_origid;
448         refer_cnt = 0;
449         hadref = simple_request = 0;
450         rc = -2;        /* default is to keep looking (no response found) */
451         lr->lr_res_msgtype = tag;
452
453         /*
454          * This code figures out if we are going to chase a
455          * referral / search reference, or pass it back to the application
456          */
457         v3ref = 0;      /* Assume not a V3 search reference or referral */
458         if( (tag != LDAP_RES_SEARCH_ENTRY) && (ld->ld_version > LDAP_VERSION2) ) {
459                 BerElement      tmpber = *ber;  /* struct copy */
460                 char **refs = NULL;
461
462                 if( tag == LDAP_RES_SEARCH_REFERENCE) {
463                         /* This is a V3 search reference */
464                         /* Assume we do not chase the reference, but pass it to application */
465                         v3ref = -1;
466                         if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ||
467                                         (lr->lr_parent != NULL) )
468                         {
469                                 /* Get the referral list */
470                                 if ( ber_scanf( &tmpber, "{v}", &refs ) == LBER_ERROR ) {
471                                         rc = LDAP_DECODING_ERROR;
472                                 } else {
473                                         /* Note: refs arrary is freed by ldap_chase_v3referrals */
474                                         refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
475                                             1, &lr->lr_res_error, &hadref );
476                                         if ( refer_cnt > 0 ) {  /* sucessfully chased reference */
477                                                 /* If haven't got end search, set chasing referrals */
478                                                 if( lr->lr_status != LDAP_REQST_COMPLETED) {
479                                                         lr->lr_status = LDAP_REQST_CHASINGREFS;
480                                                         Debug( LDAP_DEBUG_TRACE,
481                                                             "read1msg:  search ref chased, mark request chasing refs, id = %d\n",
482                                                             lr->lr_msgid, 0, 0);
483                                                 }
484                                                 v3ref = 1;      /* We sucessfully chased the reference */
485                                         }
486                                 }
487                         }
488                 } else {
489                         /* Check for V3 referral */
490                         ber_len_t len;
491                         if ( ber_scanf( &tmpber, "{iaa",/*}*/ &lderr,
492                                     &lr->lr_res_matched, &lr->lr_res_error )
493                                     != LBER_ERROR ) {
494                                 /* Check if V3 referral */
495                                 if( ber_peek_tag( &tmpber, &len) == LDAP_TAG_REFERRAL ) {
496                                         /* We have a V3 referral, assume we cannot chase it */
497                                         v3ref = -1;
498                                         if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS)
499                                                          || (lr->lr_parent != NULL) )
500                                         {
501                                                 v3ref = -1;  /* Assume referral not chased and return it to app */
502                                                 /* Get the referral list */
503                                                 if( ber_scanf( &tmpber, "{v}", &refs) == LBER_ERROR) {
504                                                         rc = LDAP_DECODING_ERROR;
505                                                         lr->lr_status = LDAP_REQST_COMPLETED;
506                                                         Debug( LDAP_DEBUG_TRACE,
507                                                             "read1msg: referral decode error, mark request completed, id = %d\n",
508                                                                     lr->lr_msgid, 0, 0);
509                                                 } else {
510                                                         /* Chase the referral 
511                                                          * Note: refs arrary is freed by ldap_chase_v3referrals
512                                                          */
513                                                         refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
514                                                             0, &lr->lr_res_error, &hadref );
515                                                         lr->lr_status = LDAP_REQST_COMPLETED;
516                                                         Debug( LDAP_DEBUG_TRACE,
517                                                             "read1msg:  referral chased, mark request completed, id = %d\n",
518                                                             lr->lr_msgid, 0, 0);
519                                                         if( refer_cnt > 0) {
520                                                                 v3ref = 1;  /* Referral successfully chased */
521                                                         }
522                                                 }
523                                         }
524                                 }
525
526                                 if( lr->lr_res_matched != NULL ) {
527                                         LDAP_FREE( lr->lr_res_matched );
528                                         lr->lr_res_matched = NULL;
529                                 }
530                                 if( lr->lr_res_error != NULL ) {
531                                         LDAP_FREE( lr->lr_res_error );
532                                         lr->lr_res_error = NULL;
533                                 }
534                         }
535                 }
536         }
537
538         /* All results that just return a status, i.e. don't return data
539          * go through the following code.  This code also chases V2 referrals
540          * and checks if all referrals have been chased.
541          */
542         if ( (tag != LDAP_RES_SEARCH_ENTRY) && (v3ref > -1) ) {
543                 /* For a v3 search referral/reference, only come here if already chased it */
544                 if ( ld->ld_version >= LDAP_VERSION2 &&
545                         ( lr->lr_parent != NULL ||
546                         LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ) )
547                 {
548                         tmpber = *ber;  /* struct copy */
549                         if ( v3ref == 1 ) {
550                                 ; /* V3 search reference or V3 referral sucessfully chased */
551                         } else if ( ber_scanf( &tmpber, "{iaa}", &lderr,
552                             &lr->lr_res_matched, &lr->lr_res_error )
553                             != LBER_ERROR ) {
554                                 if ( lderr != LDAP_SUCCESS ) {
555                                         /* referrals are in error string */
556                                         refer_cnt = ldap_chase_referrals( ld, lr,
557                                                 &lr->lr_res_error, -1, &hadref );
558                                         lr->lr_status = LDAP_REQST_COMPLETED;
559                                         Debug( LDAP_DEBUG_TRACE,
560                                             "read1msg:  V2 referral chased, mark request completed, id = %d\n", lr->lr_msgid, 0, 0);
561                                 }
562
563                                 /* save errno, message, and matched string */
564                                 if ( !hadref || lr->lr_res_error == NULL ) {
565                                         lr->lr_res_errno = ( lderr ==
566                                         LDAP_PARTIAL_RESULTS ) ? LDAP_SUCCESS
567                                         : lderr;
568                                 } else if ( ld->ld_errno != LDAP_SUCCESS ) {
569                                         lr->lr_res_errno = ld->ld_errno;
570                                 } else {
571                                         lr->lr_res_errno = LDAP_PARTIAL_RESULTS;
572                                 }
573 Debug( LDAP_DEBUG_TRACE,
574     "new result:  res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
575     lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
576     lr->lr_res_matched ? lr->lr_res_matched : "" );
577                         }
578                 }
579
580                 Debug( LDAP_DEBUG_TRACE,
581                     "read1msg:  %d new referrals\n", refer_cnt, 0, 0 );
582
583                 if ( refer_cnt != 0 ) { /* chasing referrals */
584                         ber_free( ber, 1 );
585                         ber = NULL;
586                         if ( refer_cnt < 0 ) {
587                                 return( -1 );   /* fatal error */
588                         }
589                         lr->lr_res_errno = LDAP_SUCCESS; /* sucessfully chased referral */
590                 } else {
591                         if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
592                                 /* request without any referrals */
593                                 simple_request = ( hadref ? 0 : 1 );
594                         } else {
595                                 /* request with referrals or child request */
596                                 ber_free( ber, 1 );
597                                 ber = NULL;
598                         }
599
600                         lr->lr_status = LDAP_REQST_COMPLETED; /* declare this request done */
601                         Debug( LDAP_DEBUG_TRACE,
602                             "read1msg:  mark request completed, id = %d\n", lr->lr_msgid, 0, 0);
603                         while ( lr->lr_parent != NULL ) {
604                                 merge_error_info( ld, lr->lr_parent, lr );
605
606                                 lr = lr->lr_parent;
607                                 if ( --lr->lr_outrefcnt > 0 ) {
608                                         break;  /* not completely done yet */
609                                 }
610                         }
611
612                         /* Check if all requests are finished, lr is now parent */
613                         tmplr = lr;
614                         if (tmplr->lr_status == LDAP_REQST_COMPLETED) {
615                                 for(tmplr=lr->lr_child; tmplr != NULL; tmplr=tmplr->lr_refnext) {
616                                 if( tmplr->lr_status != LDAP_REQST_COMPLETED) {
617                                         break;
618                                         }
619                                 }
620                         }
621
622                         /* This is the parent request if the request has referrals */
623                         if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL && tmplr == NULL ) {
624                                 id = lr->lr_msgid;
625                                 tag = lr->lr_res_msgtype;
626                                 Debug( LDAP_DEBUG_ANY, "request %ld done\n",
627                                     (long) id, 0, 0 );
628 Debug( LDAP_DEBUG_TRACE,
629 "res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
630 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
631 lr->lr_res_matched ? lr->lr_res_matched : "" );
632                                 if ( !simple_request ) {
633                                         ber_free( ber, 1 );
634                                         ber = NULL;
635                                         if ( build_result_ber( ld, &ber, lr )
636                                             == LBER_ERROR ) {
637                                                 rc = -1; /* fatal error */
638                                         }
639                                 }
640
641                                 ldap_free_request( ld, lr );
642                         }
643
644                         if ( lc != NULL ) {
645                                 ldap_free_connection( ld, lc, 0, 1 );
646                         }
647                 }
648         }
649
650         if ( ber == NULL ) {
651                 return( rc );
652         }
653
654         /* make a new ldap message */
655         if ( (new = (LDAPMessage *) LDAP_CALLOC( 1, sizeof(LDAPMessage) ))
656             == NULL ) {
657                 ld->ld_errno = LDAP_NO_MEMORY;
658                 return( -1 );
659         }
660         new->lm_msgid = (int)id;
661         new->lm_msgtype = tag;
662         new->lm_ber = ber;
663
664 #ifndef LDAP_NOCACHE
665                 if ( ld->ld_cache != NULL ) {
666                         ldap_add_result_to_cache( ld, new );
667                 }
668 #endif /* LDAP_NOCACHE */
669
670         /* is this the one we're looking for? */
671         if ( msgid == LDAP_RES_ANY || id == msgid ) {
672                 if ( all == LDAP_MSG_ONE
673                     || (new->lm_msgtype != LDAP_RES_SEARCH_RESULT
674                     && new->lm_msgtype != LDAP_RES_SEARCH_ENTRY
675                     && new->lm_msgtype != LDAP_RES_SEARCH_REFERENCE) ) {
676                         *result = new;
677                         ld->ld_errno = LDAP_SUCCESS;
678                         return( tag );
679                 } else if ( new->lm_msgtype == LDAP_RES_SEARCH_RESULT) {
680                         foundit = 1;    /* return the chain later */
681                 }
682         }
683
684         /* 
685          * if not, we must add it to the list of responses.  if
686          * the msgid is already there, it must be part of an existing
687          * search response.
688          */
689
690         prev = NULL;
691         for ( l = ld->ld_responses; l != NULL; l = l->lm_next ) {
692                 if ( l->lm_msgid == new->lm_msgid )
693                         break;
694                 prev = l;
695         }
696
697         /* not part of an existing search response */
698         if ( l == NULL ) {
699                 if ( foundit ) {
700                         *result = new;
701                         ld->ld_errno = LDAP_SUCCESS;
702                         return( tag );
703                 }
704
705                 new->lm_next = ld->ld_responses;
706                 ld->ld_responses = new;
707                 return( -2 );   /* continue looking */
708         }
709
710         Debug( LDAP_DEBUG_TRACE, "adding response id %ld type %ld:\n",
711             (long) new->lm_msgid, (long) new->lm_msgtype, 0 );
712
713         /* part of a search response - add to end of list of entries */
714         for ( tmp = l; (tmp->lm_chain != NULL) &&
715                 ((tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY) ||
716                  (tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_REFERENCE) ||
717                          (tmp->lm_chain->lm_msgtype == LDAP_RES_EXTENDED_PARTIAL ));
718             tmp = tmp->lm_chain )
719                 ;       /* NULL */
720         tmp->lm_chain = new;
721
722         /* return the whole chain if that's what we were looking for */
723         if ( foundit ) {
724                 if ( prev == NULL )
725                         ld->ld_responses = l->lm_next;
726                 else
727                         prev->lm_next = l->lm_next;
728                 *result = l;
729                 ld->ld_errno = LDAP_SUCCESS;
730 #ifdef LDAP_WORLD_P16
731                 /*
732                  * XXX questionable fix; see text for [P16] on
733                  * http://www.critical-angle.com/ldapworld/patch/
734                  *
735                  * inclusion of this patch causes searchs to hang on
736                  * multiple platforms
737                  */
738                 return( l->lm_msgtype );
739 #else   /* LDAP_WORLD_P16 */
740                 return( tag );
741 #endif  /* !LDAP_WORLD_P16 */
742         }
743
744         return( -2 );   /* continue looking */
745 }
746
747
748 static ber_tag_t
749 build_result_ber( LDAP *ld, BerElement **bp, LDAPRequest *lr )
750 {
751         ber_len_t       len;
752         ber_int_t       tag;
753         ber_int_t       along;
754         BerElement *ber;
755
756         *bp = NULL;
757         ber = ldap_alloc_ber_with_options( ld );
758
759         if( ber == NULL ) {
760                 ld->ld_errno = LDAP_NO_MEMORY;
761                 return LBER_ERROR;
762         }
763
764         if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
765             lr->lr_res_msgtype, lr->lr_res_errno,
766             lr->lr_res_matched ? lr->lr_res_matched : "",
767             lr->lr_res_error ? lr->lr_res_error : "" ) == -1 ) {
768
769                 ld->ld_errno = LDAP_ENCODING_ERROR;
770                 ber_free(ber, 1);
771                 return( LBER_ERROR );
772         }
773
774         ber_reset( ber, 1 );
775
776         if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) {
777                 ld->ld_errno = LDAP_DECODING_ERROR;
778                 ber_free(ber, 1);
779                 return( LBER_ERROR );
780         }
781
782         if ( ber_get_int( ber, &along ) == LBER_ERROR ) {
783                 ld->ld_errno = LDAP_DECODING_ERROR;
784                 ber_free(ber, 1);
785                 return( LBER_ERROR );
786         }
787
788         tag = ber_peek_tag( ber, &len );
789
790         if ( tag == LBER_ERROR ) {
791                 ld->ld_errno = LDAP_DECODING_ERROR;
792                 ber_free(ber, 1);
793                 return( LBER_ERROR );
794         }
795
796         *bp = ber;
797         return tag;
798 }
799
800
801 static void
802 merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
803 {
804 /*
805  * Merge error information in "lr" with "parentr" error code and string.
806  */
807         if ( lr->lr_res_errno == LDAP_PARTIAL_RESULTS ) {
808                 parentr->lr_res_errno = lr->lr_res_errno;
809                 if ( lr->lr_res_error != NULL ) {
810                         (void)ldap_append_referral( ld, &parentr->lr_res_error,
811                             lr->lr_res_error );
812                 }
813         } else if ( lr->lr_res_errno != LDAP_SUCCESS &&
814             parentr->lr_res_errno == LDAP_SUCCESS ) {
815                 parentr->lr_res_errno = lr->lr_res_errno;
816                 if ( parentr->lr_res_error != NULL ) {
817                         LDAP_FREE( parentr->lr_res_error );
818                 }
819                 parentr->lr_res_error = lr->lr_res_error;
820                 lr->lr_res_error = NULL;
821                 if ( LDAP_NAME_ERROR( lr->lr_res_errno )) {
822                         if ( parentr->lr_res_matched != NULL ) {
823                                 LDAP_FREE( parentr->lr_res_matched );
824                         }
825                         parentr->lr_res_matched = lr->lr_res_matched;
826                         lr->lr_res_matched = NULL;
827                 }
828         }
829
830         Debug( LDAP_DEBUG_TRACE, "merged parent (id %d) error info:  ",
831             parentr->lr_msgid, 0, 0 );
832         Debug( LDAP_DEBUG_TRACE, "result errno %d, error <%s>, matched <%s>\n",
833             parentr->lr_res_errno, parentr->lr_res_error ?
834             parentr->lr_res_error : "", parentr->lr_res_matched ?
835             parentr->lr_res_matched : "" );
836 }
837
838
839
840 int
841 ldap_msgtype( LDAPMessage *lm )
842 {
843         assert( lm != NULL );
844         return ( lm != NULL ) ? lm->lm_msgtype : -1;
845 }
846
847
848 int
849 ldap_msgid( LDAPMessage *lm )
850 {
851         assert( lm != NULL );
852
853         return ( lm != NULL ) ? lm->lm_msgid : -1;
854 }
855
856
857 char * ldap_int_msgtype2str( ber_tag_t tag )
858 {
859         switch( tag ) {
860         case LDAP_RES_ADD: return "add";
861         case LDAP_RES_BIND: return "bind";
862         case LDAP_RES_COMPARE: return "compare";
863         case LDAP_RES_DELETE: return "delete";
864         case LDAP_RES_EXTENDED: return "extended-result";
865         case LDAP_RES_EXTENDED_PARTIAL: return "extended-partial";
866         case LDAP_RES_MODIFY: return "modify";
867         case LDAP_RES_RENAME: return "rename";
868         case LDAP_RES_SEARCH_ENTRY: return "search-entry";
869         case LDAP_RES_SEARCH_REFERENCE: return "search-reference";
870         case LDAP_RES_SEARCH_RESULT: return "search-result";
871         }
872         return "unknown";
873 }
874
875 int
876 ldap_msgfree( LDAPMessage *lm )
877 {
878         LDAPMessage     *next;
879         int             type = 0;
880
881         Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
882
883         for ( ; lm != NULL; lm = next ) {
884                 next = lm->lm_chain;
885                 type = lm->lm_msgtype;
886                 ber_free( lm->lm_ber, 1 );
887                 LDAP_FREE( (char *) lm );
888         }
889
890         return( type );
891 }
892
893 /*
894  * ldap_msgdelete - delete a message.  It returns:
895  *      0       if the entire message was deleted
896  *      -1      if the message was not found, or only part of it was found
897  */
898 int
899 ldap_msgdelete( LDAP *ld, int msgid )
900 {
901         LDAPMessage     *lm, *prev;
902
903         assert( ld != NULL );
904
905         Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete\n", 0, 0, 0 );
906
907         prev = NULL;
908         for ( lm = ld->ld_responses; lm != NULL; lm = lm->lm_next ) {
909                 if ( lm->lm_msgid == msgid )
910                         break;
911                 prev = lm;
912         }
913
914         if ( lm == NULL )
915                 return( -1 );
916
917         if ( prev == NULL )
918                 ld->ld_responses = lm->lm_next;
919         else
920                 prev->lm_next = lm->lm_next;
921
922         if ( ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY )
923                 return( -1 );
924
925         return( 0 );
926 }
927
928
929 /*
930  * return 1 if message msgid is waiting to be abandoned, 0 otherwise
931  */
932 static int
933 ldap_abandoned( LDAP *ld, ber_int_t msgid )
934 {
935         int     i;
936
937         if ( ld->ld_abandoned == NULL )
938                 return( 0 );
939
940         for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
941                 if ( ld->ld_abandoned[i] == msgid )
942                         return( 1 );
943
944         return( 0 );
945 }
946
947
948 static int
949 ldap_mark_abandoned( LDAP *ld, ber_int_t msgid )
950 {
951         int     i;
952
953         if ( ld->ld_abandoned == NULL )
954                 return( -1 );
955
956         for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
957                 if ( ld->ld_abandoned[i] == msgid )
958                         break;
959
960         if ( ld->ld_abandoned[i] == -1 )
961                 return( -1 );
962
963         for ( ; ld->ld_abandoned[i] != -1; i++ ) {
964                 ld->ld_abandoned[i] = ld->ld_abandoned[i + 1];
965         }
966
967         return( 0 );
968 }