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