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