]> git.sur5r.net Git - openldap/blob - libraries/libldap/result.c
Suck in latest changes from HEAD
[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 #ifdef NEW_LOGGING
101         LDAP_LOG (( "result", LDAP_LEVEL_ARGS, "ldap_result msgid %d\n", msgid ));
102 #else
103         Debug( LDAP_DEBUG_TRACE, "ldap_result msgid %d\n", msgid, 0, 0 );
104 #endif
105
106         if( ld == NULL ) {
107                 return -1;
108         }
109
110         if( result == NULL ) {
111                 ld->ld_errno = LDAP_PARAM_ERROR;
112                 return -1;
113         }
114
115     lm = chkResponseList(ld, msgid, all);
116
117         if ( lm == NULL ) {
118                 return( wait4msg( ld, msgid, all, timeout, result ) );
119         }
120
121         *result = lm;
122         ld->ld_errno = LDAP_SUCCESS;
123         return( lm->lm_msgtype );
124 }
125
126 static LDAPMessage *
127 chkResponseList(
128         LDAP *ld,
129         int msgid,
130         int all)
131 {
132         LDAPMessage     *lm, *lastlm, *nextlm;
133     /*
134          * Look through the list of responses we have received on
135          * this association and see if the response we're interested in
136          * is there.  If it is, return it.  If not, call wait4msg() to
137          * wait until it arrives or timeout occurs.
138          */
139
140 #ifdef NEW_LOGGING
141         LDAP_LOG (( "result", LDAP_LEVEL_ARGS, 
142                 "ldap_chkResponseList for msgid=%d, all=%d\n", msgid, all ));
143 #else
144         Debug( LDAP_DEBUG_TRACE,
145                 "ldap_chkResponseList for msgid=%d, all=%d\n",
146             msgid, all, 0 );
147 #endif
148         lastlm = NULL;
149         for ( lm = ld->ld_responses; lm != NULL; lm = nextlm ) {
150                 nextlm = lm->lm_next;
151
152                 if ( ldap_abandoned( ld, lm->lm_msgid ) ) {
153 #ifdef NEW_LOGGING
154                         LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1, 
155                                 "ldap_chkResponseList msg abandoned, msgid %d\n", msgid ));
156 #else
157                         Debug( LDAP_DEBUG_TRACE,
158                                 "ldap_chkResponseList msg abandoned, msgid %d\n",
159                             msgid, 0, 0 );
160 #endif
161                         ldap_mark_abandoned( ld, lm->lm_msgid );
162
163                         if ( lastlm == NULL ) {
164                                 /* Remove first entry in list */
165                                 ld->ld_responses = lm->lm_next;
166                         } else {
167                                 lastlm->lm_next = nextlm;
168                         }
169
170                         ldap_msgfree( lm );
171
172                         continue;
173                 }
174
175                 if ( msgid == LDAP_RES_ANY || lm->lm_msgid == msgid ) {
176                         LDAPMessage     *tmp;
177
178                         if ( all == LDAP_MSG_ONE || msgid == LDAP_RES_UNSOLICITED ) {
179                                 break;
180                         }
181
182                         for ( tmp = lm; tmp != NULL; tmp = tmp->lm_chain ) {
183                                 if ( tmp->lm_msgtype != LDAP_RES_SEARCH_ENTRY
184                                     && tmp->lm_msgtype != LDAP_RES_SEARCH_REFERENCE
185                                         && tmp->lm_msgtype != LDAP_RES_EXTENDED_PARTIAL )
186                                 {
187                                         break;
188                                 }
189                         }
190
191                         if ( tmp == NULL ) {
192                                 lm = NULL;
193                         }
194
195                         break;
196                 }
197                 lastlm = lm;
198         }
199
200     if ( lm != NULL ) {
201                 /* Found an entry, remove it from the list */
202             if ( lastlm == NULL ) {
203                     ld->ld_responses = (all == LDAP_MSG_ONE && lm->lm_chain != NULL
204                         ? lm->lm_chain : lm->lm_next);
205             } else {
206                     lastlm->lm_next = (all == LDAP_MSG_ONE && lm->lm_chain != NULL
207                         ? lm->lm_chain : lm->lm_next);
208             }
209             if ( all == LDAP_MSG_ONE && lm->lm_chain != NULL ) {
210                     lm->lm_chain->lm_next = lm->lm_next;
211                     lm->lm_chain = NULL;
212             }
213             lm->lm_next = NULL;
214     }
215
216 #ifdef LDAP_DEBUG
217         if( lm == NULL) {
218 #ifdef NEW_LOGGING
219                 LDAP_LOG (( "result", LDAP_LEVEL_RESULTS, 
220                         "ldap_chkResponseList returns NULL\n" ));
221 #else
222                 Debug( LDAP_DEBUG_TRACE,
223                         "ldap_chkResponseList returns NULL\n", 0, 0, 0);
224 #endif
225         } else {
226 #ifdef NEW_LOGGING
227                 LDAP_LOG (( "result", LDAP_LEVEL_RESULTS, 
228                         "ldap_chkResponseList returns msgid %d, type 0x02lu\n",
229                         lm->lm_msgid, (unsigned long) lm->lm_msgtype ));
230 #else
231                 Debug( LDAP_DEBUG_TRACE,
232                         "ldap_chkResponseList returns msgid %d, type 0x%02lu\n",
233                         lm->lm_msgid, (unsigned long) lm->lm_msgtype, 0);
234 #endif
235         }
236 #endif
237     return lm;
238 }
239
240 static int
241 wait4msg(
242         LDAP *ld,
243         ber_int_t msgid,
244         int all,
245         struct timeval *timeout,
246         LDAPMessage **result )
247 {
248         int             rc;
249         struct timeval  tv, *tvp;
250         time_t          start_time = 0;
251         time_t          tmp_time;
252         LDAPConn        *lc, *nextlc;
253
254         assert( ld != NULL );
255         assert( result != NULL );
256
257 #ifdef LDAP_DEBUG
258         if ( timeout == NULL ) {
259 #ifdef NEW_LOGGING
260                 LDAP_LOG (( "result", LDAP_LEVEL_ARGS,
261                         "wait4msg (infinite timeout), msgid %d\n", msgid ));
262 #else
263                 Debug( LDAP_DEBUG_TRACE, "wait4msg (infinite timeout), msgid %d\n",
264                     msgid, 0, 0 );
265 #endif
266         } else {
267 #ifdef NEW_LOGGING
268                 LDAP_LOG (( "result", LDAP_LEVEL_ARGS,
269                         "wait4msg (timeout %ld sec, %ld usec), msgid %d\n", 
270                         (long) timeout->tv_sec, (long) timeout->tv_usec, msgid ));
271 #else
272                 Debug( LDAP_DEBUG_TRACE, "wait4msg (timeout %ld sec, %ld usec), msgid %d\n",
273                        (long) timeout->tv_sec, (long) timeout->tv_usec, msgid );
274 #endif
275         }
276 #endif /* LDAP_DEBUG */
277
278         if ( timeout == NULL ) {
279                 tvp = NULL;
280         } else {
281                 tv = *timeout;
282                 tvp = &tv;
283                 start_time = time( NULL );
284         }
285                     
286         rc = -2;
287         while ( rc == -2 ) {
288 #ifdef LDAP_DEBUG
289 #ifdef NEW_LOGGING
290                 LDAP_LOG (( "result", LDAP_LEVEL_ARGS,
291                         "wait4msg continue, msgid %d, all %d\n", msgid, all ));
292 #else
293                 Debug( LDAP_DEBUG_TRACE, "wait4msg continue, msgid %d, all %d\n",
294                     msgid, all, 0 );
295 #endif
296                 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
297                         ldap_dump_connection( ld, ld->ld_conns, 1 );
298                         ldap_dump_requests_and_responses( ld );
299                 }
300 #endif /* LDAP_DEBUG */
301
302         if( (*result = chkResponseList(ld, msgid, all)) != NULL ) {
303             rc = (*result)->lm_msgtype;
304         } else {
305
306                         for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
307                                 if ( ber_sockbuf_ctrl( lc->lconn_sb,
308                                                 LBER_SB_OPT_DATA_READY, NULL ) ) {
309                                             rc = try_read1msg( ld, msgid, all, lc->lconn_sb,
310                                                 lc, result );
311                                     break;
312                                 }
313                 }
314
315                     if ( lc == NULL ) {
316                             rc = ldap_int_select( ld, tvp );
317
318
319 #ifdef LDAP_DEBUG
320                             if ( rc == -1 ) {
321 #ifdef NEW_LOGGING
322                                         LDAP_LOG (( "result", LDAP_LEVEL_ARGS,
323                                                 "wait4msg: ldap_int_select returned -1: errno %d\n", 
324                                                 errno ));
325 #else
326                                 Debug( LDAP_DEBUG_TRACE,
327                                         "ldap_int_select returned -1: errno %d\n",
328                                         errno, 0, 0 );
329 #endif
330                             }
331 #endif
332
333                             if ( rc == 0 || ( rc == -1 && (
334                                     !LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART)
335                                     || errno != EINTR )))
336                             {
337                                     ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
338                                         LDAP_TIMEOUT);
339                                     return( rc );
340                             }
341
342                             if ( rc == -1 ) {
343                                     rc = -2;    /* select interrupted: loop */
344                             } else {
345                                     rc = -2;
346                                     for ( lc = ld->ld_conns; rc == -2 && lc != NULL;
347                                         lc = nextlc ) {
348                                             nextlc = lc->lconn_next;
349                                             if ( lc->lconn_status ==
350                                                 LDAP_CONNST_CONNECTED &&
351                                                 ldap_is_read_ready( ld,
352                                                 lc->lconn_sb )) {
353                                                     rc = try_read1msg( ld, msgid, all,
354                                                         lc->lconn_sb, lc, result );
355                                             }
356                                     }
357                             }
358                     }
359                 }
360
361                 if ( rc == -2 && tvp != NULL ) {
362                         tmp_time = time( NULL );
363                         if (( tv.tv_sec -=  ( tmp_time - start_time )) <= 0 ) {
364                                 rc = 0; /* timed out */
365                                 ld->ld_errno = LDAP_TIMEOUT;
366                                 break;
367                         }
368
369 #ifdef NEW_LOGGING
370                         LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1,
371                                 "wait4msg: %ld secs to go\n", (long) tv.tv_sec ));
372 #else
373                         Debug( LDAP_DEBUG_TRACE, "wait4msg:  %ld secs to go\n",
374                                (long) tv.tv_sec, 0, 0 );
375 #endif
376                         start_time = tmp_time;
377                 }
378         }
379
380         return( rc );
381 }
382
383
384 static ber_tag_t
385 try_read1msg(
386         LDAP *ld,
387         ber_int_t msgid,
388         int all,
389         Sockbuf *sb,
390         LDAPConn *lc,
391         LDAPMessage **result )
392 {
393         BerElement      *ber;
394         LDAPMessage     *new, *l, *prev, *tmp;
395         ber_int_t       id;
396         ber_tag_t       tag;
397         ber_len_t       len;
398         int             foundit = 0;
399         LDAPRequest     *lr, *tmplr;
400         BerElement      tmpber;
401         int             rc, refer_cnt, hadref, simple_request;
402         ber_int_t       lderr;
403         /*
404          * v3ref = flag for V3 referral / search reference
405          * 0 = not a ref, 1 = sucessfully chased ref, -1 = pass ref to application
406          */
407         int     v3ref;
408
409         assert( ld != NULL );
410         assert( lc != NULL );
411         
412 #ifdef NEW_LOGGING
413         LDAP_LOG (( "result", LDAP_LEVEL_ARGS, "read1msg: msgid %d, all %d\n",
414                 msgid, all ));
415 #else
416         Debug( LDAP_DEBUG_TRACE, "read1msg: msgid %d, all %d\n", msgid, all, 0 );
417 #endif
418
419     if ( lc->lconn_ber == NULL ) {
420                 lc->lconn_ber = ldap_alloc_ber_with_options(ld);
421
422                 if( lc->lconn_ber == NULL ) {
423                         return -1;
424                 }
425     }
426
427         ber = lc->lconn_ber;
428         assert( LBER_VALID (ber) );
429
430         /* get the next message */
431         errno = 0;
432 #ifdef LDAP_CONNECTIONLESS
433         if ( LDAP_IS_UDP(ld) ) {
434                 struct sockaddr from;
435                 ber_int_sb_read(sb, &from, sizeof(struct sockaddr));
436         }
437 #endif
438         if ( (tag = ber_get_next( sb, &len, ber ))
439             != LDAP_TAG_MESSAGE ) {
440                 if ( tag == LBER_DEFAULT) {
441 #ifdef LDAP_DEBUG                  
442 #ifdef NEW_LOGGING
443                         LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1, 
444                                 "read1msg: ber_get_next failed\n" ));
445 #else
446                         Debug( LDAP_DEBUG_CONNS,
447                               "ber_get_next failed.\n", 0, 0, 0 );
448 #endif             
449 #endif             
450 #ifdef EWOULDBLOCK                      
451                         if (errno==EWOULDBLOCK) return -2;
452 #endif
453 #ifdef EAGAIN
454                         if (errno == EAGAIN) return -2;
455 #endif
456                         ld->ld_errno = LDAP_SERVER_DOWN;
457                         return -1;
458                 }
459                 ld->ld_errno = LDAP_LOCAL_ERROR;
460                 return -1;
461         }
462
463         /*
464      * We read a complete message.
465          * The connection should no longer need this ber.
466          */
467     lc->lconn_ber = NULL;
468
469         /* message id */
470         if ( ber_get_int( ber, &id ) == LBER_ERROR ) {
471                 ber_free( ber, 1 );
472                 ld->ld_errno = LDAP_DECODING_ERROR;
473                 return( -1 );
474         }
475
476         /* if it's been abandoned, toss it */
477         if ( ldap_abandoned( ld, id ) ) {
478                 ber_free( ber, 1 );
479 #ifdef NEW_LOGGING
480                 LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1, 
481                         "read1msg: abandoned\n" ));
482 #else
483                 Debug( LDAP_DEBUG_ANY, "abandoned\n", 0, 0, 0);
484 #endif
485                 return( -2 );   /* continue looking */
486         }
487
488         if (( lr = ldap_find_request_by_msgid( ld, id )) == NULL ) {
489 #ifdef NEW_LOGGING
490                 LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1, 
491                         "read1msg: no request for response with msgid %ld (tossing)\n",
492                         (long) id ));
493 #else
494                 Debug( LDAP_DEBUG_ANY,
495                     "no request for response with msgid %ld (tossing)\n",
496                     (long) id, 0, 0 );
497 #endif
498                 ber_free( ber, 1 );
499                 return( -2 );   /* continue looking */
500         }
501 #ifdef LDAP_CONNECTIONLESS
502         if (LDAP_IS_UDP(ld) && ld->ld_options.ldo_version == LDAP_VERSION2) {
503                 struct berval blank;
504                 ber_scanf(ber, "m{", &blank);
505         }
506 #endif
507         /* the message type */
508         if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
509                 ld->ld_errno = LDAP_DECODING_ERROR;
510                 ber_free( ber, 1 );
511                 return( -1 );
512         }
513
514 #ifdef NEW_LOGGING
515         LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1, 
516                 "read1msg: ldap_read: message type %s msgid %ld, original id %ld\n",
517             ldap_int_msgtype2str( tag ),
518                 (long) lr->lr_msgid, (long) lr->lr_origid ));
519 #else
520         Debug( LDAP_DEBUG_TRACE,
521                 "ldap_read: message type %s msgid %ld, original id %ld\n",
522             ldap_int_msgtype2str( tag ),
523                 (long) lr->lr_msgid, (long) lr->lr_origid );
524 #endif
525
526         id = lr->lr_origid;
527         refer_cnt = 0;
528         hadref = simple_request = 0;
529         rc = -2;        /* default is to keep looking (no response found) */
530         lr->lr_res_msgtype = tag;
531
532         /*
533          * This code figures out if we are going to chase a
534          * referral / search reference, or pass it back to the application
535          */
536         v3ref = 0;      /* Assume not a V3 search reference or referral */
537         if( (tag != LDAP_RES_SEARCH_ENTRY) && (ld->ld_version > LDAP_VERSION2) ) {
538                 BerElement      tmpber = *ber;  /* struct copy */
539                 char **refs = NULL;
540
541                 if( tag == LDAP_RES_SEARCH_REFERENCE) {
542                         /* This is a V3 search reference */
543                         /* Assume we do not chase the reference, but pass it to application */
544                         v3ref = -1;
545                         if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ||
546                                         (lr->lr_parent != NULL) )
547                         {
548                                 /* Get the referral list */
549                                 if ( ber_scanf( &tmpber, "{v}", &refs ) == LBER_ERROR ) {
550                                         rc = LDAP_DECODING_ERROR;
551                                 } else {
552                                         /* Note: refs arrary is freed by ldap_chase_v3referrals */
553                                         refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
554                                             1, &lr->lr_res_error, &hadref );
555                                         if ( refer_cnt > 0 ) {  /* sucessfully chased reference */
556                                                 /* If haven't got end search, set chasing referrals */
557                                                 if( lr->lr_status != LDAP_REQST_COMPLETED) {
558                                                         lr->lr_status = LDAP_REQST_CHASINGREFS;
559 #ifdef NEW_LOGGING
560                                                         LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1, 
561                                                                 "read1msg: search ref chased,"
562                                                                 "mark request chasing refs, id =        %d\n",
563                                                                 lr->lr_msgid ));
564 #else
565                                                         Debug( LDAP_DEBUG_TRACE,
566                                                             "read1msg:  search ref chased, mark request chasing refs, id = %d\n",
567                                                             lr->lr_msgid, 0, 0);
568 #endif
569                                                 }
570                                                 v3ref = 1;      /* We sucessfully chased the reference */
571                                         }
572                                 }
573                         }
574                 } else {
575                         /* Check for V3 referral */
576                         ber_len_t len;
577                         if ( ber_scanf( &tmpber, "{iaa",/*}*/ &lderr,
578                                     &lr->lr_res_matched, &lr->lr_res_error )
579                                     != LBER_ERROR ) {
580                                 /* Check if V3 referral */
581                                 if( ber_peek_tag( &tmpber, &len) == LDAP_TAG_REFERRAL ) {
582                                         /* We have a V3 referral, assume we cannot chase it */
583                                         v3ref = -1;
584                                         if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS)
585                                                          || (lr->lr_parent != NULL) )
586                                         {
587                                                 v3ref = -1;  /* Assume referral not chased and return it to app */
588                                                 /* Get the referral list */
589                                                 if( ber_scanf( &tmpber, "{v}", &refs) == LBER_ERROR) {
590                                                         rc = LDAP_DECODING_ERROR;
591                                                         lr->lr_status = LDAP_REQST_COMPLETED;
592 #ifdef NEW_LOGGING
593                                                         LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1, 
594                                                                 "read1msg: referral decode error,"
595                                                                 "mark request completed, id =   %d\n",
596                                                                 lr->lr_msgid ));
597 #else
598                                                         Debug( LDAP_DEBUG_TRACE,
599                                                             "read1msg: referral decode error, mark request completed, id = %d\n",
600                                                                     lr->lr_msgid, 0, 0);
601 #endif
602                                                 } else {
603                                                         /* Chase the referral 
604                                                          * Note: refs arrary is freed by ldap_chase_v3referrals
605                                                          */
606                                                         refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
607                                                             0, &lr->lr_res_error, &hadref );
608                                                         lr->lr_status = LDAP_REQST_COMPLETED;
609 #ifdef NEW_LOGGING
610                                                         LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1, 
611                                                                 "read1msg: referral chased,"
612                                                                 "mark request completed, id =   %d\n",
613                                                                 lr->lr_msgid ));
614 #else
615                                                         Debug( LDAP_DEBUG_TRACE,
616                                                             "read1msg:  referral chased, mark request completed, id = %d\n",
617                                                             lr->lr_msgid, 0, 0);
618 #endif
619                                                         if( refer_cnt > 0) {
620                                                                 v3ref = 1;  /* Referral successfully chased */
621                                                         }
622                                                 }
623                                         }
624                                 }
625
626                                 if( lr->lr_res_matched != NULL ) {
627                                         LDAP_FREE( lr->lr_res_matched );
628                                         lr->lr_res_matched = NULL;
629                                 }
630                                 if( lr->lr_res_error != NULL ) {
631                                         LDAP_FREE( lr->lr_res_error );
632                                         lr->lr_res_error = NULL;
633                                 }
634                         }
635                 }
636         }
637
638         /* All results that just return a status, i.e. don't return data
639          * go through the following code.  This code also chases V2 referrals
640          * and checks if all referrals have been chased.
641          */
642         if ( (tag != LDAP_RES_SEARCH_ENTRY) && (v3ref > -1) ) {
643                 /* For a v3 search referral/reference, only come here if already chased it */
644                 if ( ld->ld_version >= LDAP_VERSION2 &&
645                         ( lr->lr_parent != NULL ||
646                         LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ) )
647                 {
648                         tmpber = *ber;  /* struct copy */
649                         if ( v3ref == 1 ) {
650                                 /* V3 search reference or V3 referral
651                                  * sucessfully chased. If this message
652                                  * is a search result, then it has no more
653                                  * outstanding referrals.
654                                  */
655                                 if ( tag == LDAP_RES_SEARCH_RESULT )
656                                         refer_cnt = 0;
657                         } else if ( ber_scanf( &tmpber, "{iaa}", &lderr,
658                             &lr->lr_res_matched, &lr->lr_res_error )
659                             != LBER_ERROR ) {
660                                 if ( lderr != LDAP_SUCCESS ) {
661                                         /* referrals are in error string */
662                                         refer_cnt = ldap_chase_referrals( ld, lr,
663                                                 &lr->lr_res_error, -1, &hadref );
664                                         lr->lr_status = LDAP_REQST_COMPLETED;
665 #ifdef NEW_LOGGING
666                                         LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1, 
667                                                 "read1msg: V2 referral chased,"
668                                                 "mark request completed, id =   %d\n",
669                                                 lr->lr_msgid ));
670 #else
671                                         Debug( LDAP_DEBUG_TRACE,
672                                             "read1msg:  V2 referral chased, mark request completed, id = %d\n", lr->lr_msgid, 0, 0);
673 #endif
674                                 }
675
676                                 /* save errno, message, and matched string */
677                                 if ( !hadref || lr->lr_res_error == NULL ) {
678                                         lr->lr_res_errno = ( lderr ==
679                                         LDAP_PARTIAL_RESULTS ) ? LDAP_SUCCESS
680                                         : lderr;
681                                 } else if ( ld->ld_errno != LDAP_SUCCESS ) {
682                                         lr->lr_res_errno = ld->ld_errno;
683                                 } else {
684                                         lr->lr_res_errno = LDAP_PARTIAL_RESULTS;
685                                 }
686 #ifdef NEW_LOGGING
687 LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1, 
688         "read1msg: new result: res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
689     lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
690     lr->lr_res_matched ? lr->lr_res_matched : "" ));
691 #else
692 Debug( LDAP_DEBUG_TRACE,
693     "new result:  res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
694     lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
695     lr->lr_res_matched ? lr->lr_res_matched : "" );
696 #endif
697                         }
698                 }
699
700 #ifdef NEW_LOGGING
701                 LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1, 
702                         "read1msg: %d new referrals\n", refer_cnt ));
703 #else
704                 Debug( LDAP_DEBUG_TRACE,
705                     "read1msg:  %d new referrals\n", refer_cnt, 0, 0 );
706 #endif
707
708                 if ( refer_cnt != 0 ) { /* chasing referrals */
709                         ber_free( ber, 1 );
710                         ber = NULL;
711                         if ( refer_cnt < 0 ) {
712                                 return( -1 );   /* fatal error */
713                         }
714                         lr->lr_res_errno = LDAP_SUCCESS; /* sucessfully chased referral */
715                 } else {
716                         if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
717                                 /* request without any referrals */
718                                 simple_request = ( hadref ? 0 : 1 );
719                         } else {
720                                 /* request with referrals or child request */
721                                 ber_free( ber, 1 );
722                                 ber = NULL;
723                         }
724
725                         lr->lr_status = LDAP_REQST_COMPLETED; /* declare this request done */
726 #ifdef NEW_LOGGING
727                         LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1, 
728                                 "read1msg: mark request completed, id = %d\n", lr->lr_msgid ));
729 #else
730                         Debug( LDAP_DEBUG_TRACE,
731                             "read1msg:  mark request completed, id = %d\n", lr->lr_msgid, 0, 0);
732 #endif
733                         while ( lr->lr_parent != NULL ) {
734                                 merge_error_info( ld, lr->lr_parent, lr );
735
736                                 lr = lr->lr_parent;
737                                 if ( --lr->lr_outrefcnt > 0 ) {
738                                         break;  /* not completely done yet */
739                                 }
740                         }
741
742                         /* Check if all requests are finished, lr is now parent */
743                         tmplr = lr;
744                         if (tmplr->lr_status == LDAP_REQST_COMPLETED) {
745                                 for(tmplr=lr->lr_child; tmplr != NULL; tmplr=tmplr->lr_refnext) {
746                                 if( tmplr->lr_status != LDAP_REQST_COMPLETED) {
747                                         break;
748                                         }
749                                 }
750                         }
751
752                         /* This is the parent request if the request has referrals */
753                         if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL && tmplr == NULL ) {
754                                 id = lr->lr_msgid;
755                                 tag = lr->lr_res_msgtype;
756 #ifdef NEW_LOGGING
757                         LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1, 
758                                 "read1msg: request %ld done\n", (long) id ));
759                         LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1, 
760                                 "read1msg: res_errno: %d,res_error: <%s>, res_matched: <%s>\n",
761                                 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
762                                 lr->lr_res_matched ? lr->lr_res_matched : "" ));
763 #else
764                                 Debug( LDAP_DEBUG_ANY, "request %ld done\n",
765                                     (long) id, 0, 0 );
766 Debug( LDAP_DEBUG_TRACE,
767 "res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
768 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
769 lr->lr_res_matched ? lr->lr_res_matched : "" );
770 #endif
771                                 if ( !simple_request ) {
772                                         ber_free( ber, 1 );
773                                         ber = NULL;
774                                         if ( build_result_ber( ld, &ber, lr )
775                                             == LBER_ERROR ) {
776                                                 rc = -1; /* fatal error */
777                                         }
778                                 }
779
780                                 ldap_free_request( ld, lr );
781                         }
782
783                         if ( lc != NULL ) {
784                                 ldap_free_connection( ld, lc, 0, 1 );
785                         }
786                 }
787         }
788
789         if ( ber == NULL ) {
790                 return( rc );
791         }
792
793         /* make a new ldap message */
794         if ( (new = (LDAPMessage *) LDAP_CALLOC( 1, sizeof(LDAPMessage) ))
795             == NULL ) {
796                 ld->ld_errno = LDAP_NO_MEMORY;
797                 return( -1 );
798         }
799         new->lm_msgid = (int)id;
800         new->lm_msgtype = tag;
801         new->lm_ber = ber;
802
803 #ifndef LDAP_NOCACHE
804                 if ( ld->ld_cache != NULL ) {
805                         ldap_add_result_to_cache( ld, new );
806                 }
807 #endif /* LDAP_NOCACHE */
808
809         /* is this the one we're looking for? */
810         if ( msgid == LDAP_RES_ANY || id == msgid ) {
811                 if ( all == LDAP_MSG_ONE
812                     || (new->lm_msgtype != LDAP_RES_SEARCH_RESULT
813                     && new->lm_msgtype != LDAP_RES_SEARCH_ENTRY
814                     && new->lm_msgtype != LDAP_RES_SEARCH_REFERENCE) ) {
815                         *result = new;
816                         ld->ld_errno = LDAP_SUCCESS;
817                         return( tag );
818                 } else if ( new->lm_msgtype == LDAP_RES_SEARCH_RESULT) {
819                         foundit = 1;    /* return the chain later */
820                 }
821         }
822
823         /* 
824          * if not, we must add it to the list of responses.  if
825          * the msgid is already there, it must be part of an existing
826          * search response.
827          */
828
829         prev = NULL;
830         for ( l = ld->ld_responses; l != NULL; l = l->lm_next ) {
831                 if ( l->lm_msgid == new->lm_msgid )
832                         break;
833                 prev = l;
834         }
835
836         /* not part of an existing search response */
837         if ( l == NULL ) {
838                 if ( foundit ) {
839                         *result = new;
840                         ld->ld_errno = LDAP_SUCCESS;
841                         return( tag );
842                 }
843
844                 new->lm_next = ld->ld_responses;
845                 ld->ld_responses = new;
846                 return( -2 );   /* continue looking */
847         }
848
849 #ifdef NEW_LOGGING
850         LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1, 
851                 "read1msg: adding response id %ld type %ld\n",
852                 (long) new->lm_msgid, (long) new->lm_msgtype ));
853 #else
854         Debug( LDAP_DEBUG_TRACE, "adding response id %ld type %ld:\n",
855             (long) new->lm_msgid, (long) new->lm_msgtype, 0 );
856 #endif
857
858         /* part of a search response - add to end of list of entries */
859         for ( tmp = l; (tmp->lm_chain != NULL) &&
860                 ((tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY) ||
861                  (tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_REFERENCE) ||
862                          (tmp->lm_chain->lm_msgtype == LDAP_RES_EXTENDED_PARTIAL ));
863             tmp = tmp->lm_chain )
864                 ;       /* NULL */
865         tmp->lm_chain = new;
866
867         /* return the whole chain if that's what we were looking for */
868         if ( foundit ) {
869                 if ( prev == NULL )
870                         ld->ld_responses = l->lm_next;
871                 else
872                         prev->lm_next = l->lm_next;
873                 *result = l;
874                 ld->ld_errno = LDAP_SUCCESS;
875 #ifdef LDAP_WORLD_P16
876                 /*
877                  * XXX questionable fix; see text for [P16] on
878                  * http://www.critical-angle.com/ldapworld/patch/
879                  *
880                  * inclusion of this patch causes searchs to hang on
881                  * multiple platforms
882                  */
883                 return( l->lm_msgtype );
884 #else   /* LDAP_WORLD_P16 */
885                 return( tag );
886 #endif  /* !LDAP_WORLD_P16 */
887         }
888
889         return( -2 );   /* continue looking */
890 }
891
892
893 static ber_tag_t
894 build_result_ber( LDAP *ld, BerElement **bp, LDAPRequest *lr )
895 {
896         ber_len_t       len;
897         ber_tag_t       tag;
898         ber_int_t       along;
899         BerElement *ber;
900
901         *bp = NULL;
902         ber = ldap_alloc_ber_with_options( ld );
903
904         if( ber == NULL ) {
905                 ld->ld_errno = LDAP_NO_MEMORY;
906                 return LBER_ERROR;
907         }
908
909         if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
910             lr->lr_res_msgtype, lr->lr_res_errno,
911             lr->lr_res_matched ? lr->lr_res_matched : "",
912             lr->lr_res_error ? lr->lr_res_error : "" ) == -1 ) {
913
914                 ld->ld_errno = LDAP_ENCODING_ERROR;
915                 ber_free(ber, 1);
916                 return( LBER_ERROR );
917         }
918
919         ber_reset( ber, 1 );
920
921         if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) {
922                 ld->ld_errno = LDAP_DECODING_ERROR;
923                 ber_free(ber, 1);
924                 return( LBER_ERROR );
925         }
926
927         if ( ber_get_int( ber, &along ) == LBER_ERROR ) {
928                 ld->ld_errno = LDAP_DECODING_ERROR;
929                 ber_free(ber, 1);
930                 return( LBER_ERROR );
931         }
932
933         tag = ber_peek_tag( ber, &len );
934
935         if ( tag == LBER_ERROR ) {
936                 ld->ld_errno = LDAP_DECODING_ERROR;
937                 ber_free(ber, 1);
938                 return( LBER_ERROR );
939         }
940
941         *bp = ber;
942         return tag;
943 }
944
945
946 static void
947 merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
948 {
949 /*
950  * Merge error information in "lr" with "parentr" error code and string.
951  */
952         if ( lr->lr_res_errno == LDAP_PARTIAL_RESULTS ) {
953                 parentr->lr_res_errno = lr->lr_res_errno;
954                 if ( lr->lr_res_error != NULL ) {
955                         (void)ldap_append_referral( ld, &parentr->lr_res_error,
956                             lr->lr_res_error );
957                 }
958         } else if ( lr->lr_res_errno != LDAP_SUCCESS &&
959             parentr->lr_res_errno == LDAP_SUCCESS ) {
960                 parentr->lr_res_errno = lr->lr_res_errno;
961                 if ( parentr->lr_res_error != NULL ) {
962                         LDAP_FREE( parentr->lr_res_error );
963                 }
964                 parentr->lr_res_error = lr->lr_res_error;
965                 lr->lr_res_error = NULL;
966                 if ( LDAP_NAME_ERROR( lr->lr_res_errno )) {
967                         if ( parentr->lr_res_matched != NULL ) {
968                                 LDAP_FREE( parentr->lr_res_matched );
969                         }
970                         parentr->lr_res_matched = lr->lr_res_matched;
971                         lr->lr_res_matched = NULL;
972                 }
973         }
974
975 #ifdef NEW_LOGGING
976         LDAP_LOG (( "result", LDAP_LEVEL_DETAIL1, 
977                 "read1msg: merged parent (id %d) error info: result errno %d, "
978                 "error <%s>, matched <%s>\n", parentr->lr_msgid,
979             parentr->lr_res_errno, parentr->lr_res_error ?
980             parentr->lr_res_error : "", parentr->lr_res_matched ?
981             parentr->lr_res_matched : "" ));
982 #else
983         Debug( LDAP_DEBUG_TRACE, "merged parent (id %d) error info:  ",
984             parentr->lr_msgid, 0, 0 );
985         Debug( LDAP_DEBUG_TRACE, "result errno %d, error <%s>, matched <%s>\n",
986             parentr->lr_res_errno, parentr->lr_res_error ?
987             parentr->lr_res_error : "", parentr->lr_res_matched ?
988             parentr->lr_res_matched : "" );
989 #endif
990 }
991
992
993
994 int
995 ldap_msgtype( LDAPMessage *lm )
996 {
997         assert( lm != NULL );
998         return ( lm != NULL ) ? lm->lm_msgtype : -1;
999 }
1000
1001
1002 int
1003 ldap_msgid( LDAPMessage *lm )
1004 {
1005         assert( lm != NULL );
1006
1007         return ( lm != NULL ) ? lm->lm_msgid : -1;
1008 }
1009
1010
1011 char * ldap_int_msgtype2str( ber_tag_t tag )
1012 {
1013         switch( tag ) {
1014         case LDAP_RES_ADD: return "add";
1015         case LDAP_RES_BIND: return "bind";
1016         case LDAP_RES_COMPARE: return "compare";
1017         case LDAP_RES_DELETE: return "delete";
1018         case LDAP_RES_EXTENDED: return "extended-result";
1019         case LDAP_RES_EXTENDED_PARTIAL: return "extended-partial";
1020         case LDAP_RES_MODIFY: return "modify";
1021         case LDAP_RES_RENAME: return "rename";
1022         case LDAP_RES_SEARCH_ENTRY: return "search-entry";
1023         case LDAP_RES_SEARCH_REFERENCE: return "search-reference";
1024         case LDAP_RES_SEARCH_RESULT: return "search-result";
1025         }
1026         return "unknown";
1027 }
1028
1029 int
1030 ldap_msgfree( LDAPMessage *lm )
1031 {
1032         LDAPMessage     *next;
1033         int             type = 0;
1034
1035 #ifdef NEW_LOGGING
1036         LDAP_LOG (( "result", LDAP_LEVEL_ENTRY, "ldap_msgfree\n" ));
1037 #else
1038         Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
1039 #endif
1040
1041         for ( ; lm != NULL; lm = next ) {
1042                 next = lm->lm_chain;
1043                 type = lm->lm_msgtype;
1044                 ber_free( lm->lm_ber, 1 );
1045                 LDAP_FREE( (char *) lm );
1046         }
1047
1048         return( type );
1049 }
1050
1051 /*
1052  * ldap_msgdelete - delete a message.  It returns:
1053  *      0       if the entire message was deleted
1054  *      -1      if the message was not found, or only part of it was found
1055  */
1056 int
1057 ldap_msgdelete( LDAP *ld, int msgid )
1058 {
1059         LDAPMessage     *lm, *prev;
1060
1061         assert( ld != NULL );
1062
1063 #ifdef NEW_LOGGING
1064         LDAP_LOG (( "result", LDAP_LEVEL_ENTRY, "ldap_msgdelete\n" ));
1065 #else
1066         Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete\n", 0, 0, 0 );
1067 #endif
1068
1069         prev = NULL;
1070         for ( lm = ld->ld_responses; lm != NULL; lm = lm->lm_next ) {
1071                 if ( lm->lm_msgid == msgid )
1072                         break;
1073                 prev = lm;
1074         }
1075
1076         if ( lm == NULL )
1077                 return( -1 );
1078
1079         if ( prev == NULL )
1080                 ld->ld_responses = lm->lm_next;
1081         else
1082                 prev->lm_next = lm->lm_next;
1083
1084         if ( ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY )
1085                 return( -1 );
1086
1087         return( 0 );
1088 }
1089
1090
1091 /*
1092  * return 1 if message msgid is waiting to be abandoned, 0 otherwise
1093  */
1094 static int
1095 ldap_abandoned( LDAP *ld, ber_int_t msgid )
1096 {
1097         int     i;
1098
1099         if ( ld->ld_abandoned == NULL )
1100                 return( 0 );
1101
1102         for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
1103                 if ( ld->ld_abandoned[i] == msgid )
1104                         return( 1 );
1105
1106         return( 0 );
1107 }
1108
1109
1110 static int
1111 ldap_mark_abandoned( LDAP *ld, ber_int_t msgid )
1112 {
1113         int     i;
1114
1115         if ( ld->ld_abandoned == NULL )
1116                 return( -1 );
1117
1118         for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
1119                 if ( ld->ld_abandoned[i] == msgid )
1120                         break;
1121
1122         if ( ld->ld_abandoned[i] == -1 )
1123                 return( -1 );
1124
1125         for ( ; ld->ld_abandoned[i] != -1; i++ ) {
1126                 ld->ld_abandoned[i] = ld->ld_abandoned[i + 1];
1127         }
1128
1129         return( 0 );
1130 }