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