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