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