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