]> git.sur5r.net Git - openldap/blob - libraries/libldap/result.c
Assert pointer args
[openldap] / libraries / libldap / result.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 /*  Portions
6  *  Copyright (c) 1990 Regents of the University of Michigan.
7  *  All rights reserved.
8  *
9  *  result.c - wait for an ldap result
10  */
11
12 #include "portable.h"
13
14 #include <stdio.h>
15 #include <stdlib.h>
16
17 #include <ac/errno.h>
18 #include <ac/socket.h>
19 #include <ac/string.h>
20 #include <ac/time.h>
21 #include <ac/unistd.h>
22
23 #include "ldap-int.h"
24
25
26 static int ldap_abandoned LDAP_P(( LDAP *ld, int msgid ));
27 static int ldap_mark_abandoned LDAP_P(( LDAP *ld, int msgid ));
28 static int wait4msg LDAP_P(( LDAP *ld, int msgid, int all, struct timeval *timeout,
29         LDAPMessage **result ));
30 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
31 static int try_read1msg LDAP_P(( LDAP *ld, int msgid, int all, Sockbuf *sb, LDAPConn *lc,
32         LDAPMessage **result ));
33 static unsigned long build_result_ber LDAP_P(( LDAP *ld, BerElement *ber, LDAPRequest *lr ));
34 static void merge_error_info LDAP_P(( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr ));
35 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
36 static int try_read1msg LDAP_P(( LDAP *ld, int msgid, int all, Sockbuf *sb,
37         LDAPMessage **result ));
38 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
39 #if defined( LDAP_CONNECTIONLESS ) || !defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS )
40 static int ldap_select1 LDAP_P(( LDAP *ld, struct timeval *timeout ));
41 #endif
42
43
44 /*
45  * ldap_result - wait for an ldap result response to a message from the
46  * ldap server.  If msgid is -1, any message will be accepted, otherwise
47  * ldap_result will wait for a response with msgid.  If all is 0 the
48  * first message with id msgid will be accepted, otherwise, ldap_result
49  * will wait for all responses with id msgid and then return a pointer to
50  * the entire list of messages.  This is only useful for search responses,
51  * which can be of two message types (zero or more entries, followed by an
52  * ldap result).  The type of the first message received is returned.
53  * When waiting, any messages that have been abandoned are discarded.
54  *
55  * Example:
56  *      ldap_result( s, msgid, all, timeout, result )
57  */
58 int
59 ldap_result( LDAP *ld, int msgid, int all, struct timeval *timeout,
60         LDAPMessage **result )
61 {
62         LDAPMessage     *lm, *lastlm, *nextlm;
63
64         assert( ld != NULL );
65         assert( result != NULL );
66
67         Debug( LDAP_DEBUG_TRACE, "ldap_result\n", 0, 0, 0 );
68
69         if( ld == NULL ) {
70                 return -1;
71         }
72
73         if( result == NULL ) {
74                 ld->ld_errno = LDAP_PARAM_ERROR;
75                 return -1;
76         }
77
78         /*
79          * First, look through the list of responses we have received on
80          * this association and see if the response we're interested in
81          * is there.  If it is, return it.  If not, call wait4msg() to
82          * wait until it arrives or timeout occurs.
83          */
84
85         *result = NULLMSG;
86         lastlm = NULLMSG;
87         for ( lm = ld->ld_responses; lm != NULLMSG; lm = nextlm ) {
88                 nextlm = lm->lm_next;
89
90                 if ( ldap_abandoned( ld, lm->lm_msgid ) ) {
91                         ldap_mark_abandoned( ld, lm->lm_msgid );
92
93                         if ( lastlm == NULLMSG ) {
94                                 ld->ld_responses = lm->lm_next;
95                         } else {
96                                 lastlm->lm_next = nextlm;
97                         }
98
99                         ldap_msgfree( lm );
100
101                         continue;
102                 }
103
104                 if ( msgid == LDAP_RES_ANY || lm->lm_msgid == msgid ) {
105                         LDAPMessage     *tmp;
106
107                         if ( all == 0
108                             || (lm->lm_msgtype != LDAP_RES_SEARCH_RESULT
109                             && lm->lm_msgtype != LDAP_RES_SEARCH_REFERENCE      /* LDAPv3 */
110                             && lm->lm_msgtype != LDAP_RES_SEARCH_ENTRY) )
111                                 break;
112
113                         for ( tmp = lm; tmp != NULLMSG; tmp = tmp->lm_chain ) {
114                                 if ( tmp->lm_msgtype == LDAP_RES_SEARCH_RESULT )
115                                         break;
116                         }
117
118                         if ( tmp == NULLMSG ) {
119                                 return( wait4msg( ld, msgid, all, timeout,
120                                     result ) );
121                         }
122
123                         break;
124                 }
125                 lastlm = lm;
126         }
127         if ( lm == NULLMSG ) {
128                 return( wait4msg( ld, msgid, all, timeout, result ) );
129         }
130
131         if ( lastlm == NULLMSG ) {
132                 ld->ld_responses = (all == 0 && lm->lm_chain != NULLMSG
133                     ? lm->lm_chain : lm->lm_next);
134         } else {
135                 lastlm->lm_next = (all == 0 && lm->lm_chain != NULLMSG
136                     ? lm->lm_chain : lm->lm_next);
137         }
138         if ( all == 0 )
139                 lm->lm_chain = NULLMSG;
140         lm->lm_next = NULLMSG;
141
142         *result = lm;
143         ld->ld_errno = LDAP_SUCCESS;
144         return( lm->lm_msgtype );
145 }
146
147 static int
148 wait4msg( LDAP *ld, int msgid, int all, struct timeval *timeout,
149         LDAPMessage **result )
150 {
151         int             rc;
152         struct timeval  tv, *tvp;
153         time_t          start_time = 0;
154         time_t          tmp_time;
155 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
156         LDAPConn        *lc, *nextlc;
157 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
158
159         assert( ld != NULL );
160         assert( result != NULL );
161
162 #ifdef LDAP_DEBUG
163         if ( timeout == NULL ) {
164                 Debug( LDAP_DEBUG_TRACE, "wait4msg (infinite timeout)\n",
165                     0, 0, 0 );
166         } else {
167                 Debug( LDAP_DEBUG_TRACE, "wait4msg (timeout %ld sec, %ld usec)\n",
168                        (long) timeout->tv_sec, (long) timeout->tv_usec, 0 );
169         }
170 #endif /* LDAP_DEBUG */
171
172         if ( timeout == NULL ) {
173                 tvp = NULL;
174         } else {
175                 tv = *timeout;
176                 tvp = &tv;
177                 start_time = time( NULL );
178         }
179                     
180         rc = -2;
181         while ( rc == -2 ) {
182 #ifndef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
183                 /* hack attack */
184                 if ( ! ber_pvt_sb_data_ready(&ld->ld_sb) ) {
185                         rc = ldap_select1( ld, tvp );
186
187                         if ( rc == 0 || ( rc == -1 && (
188                                 ( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART)
189                                         == LDAP_OPT_OFF )
190                             || errno != EINTR ))) {
191                                 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
192                                     LDAP_TIMEOUT);
193                                 return( rc );
194                         }
195
196                 }
197                 if ( rc == -1 ) {
198                         rc = -2;        /* select interrupted: loop */
199                 } else {
200                         rc = try_read1msg( ld, msgid, all, &ld->ld_sb, result );
201                 }
202 #else /* !LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
203 #ifdef LDAP_DEBUG
204                 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
205                         ldap_dump_connection( ld, ld->ld_conns, 1 );
206                         ldap_dump_requests_and_responses( ld );
207                 }
208 #endif /* LDAP_DEBUG */
209                 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
210                         if ( ber_pvt_sb_data_ready(lc->lconn_sb) ) {
211                                 rc = try_read1msg( ld, msgid, all, lc->lconn_sb,
212                                     lc, result );
213                                 break;
214                         }
215                 }
216
217                 if ( lc == NULL ) {
218                         rc = do_ldap_select( ld, tvp );
219
220
221 #ifdef LDAP_DEBUG
222                         if ( rc == -1 ) {
223                             Debug( LDAP_DEBUG_TRACE,
224                                     "do_ldap_select returned -1: errno %d\n",
225                                     errno, 0, 0 );
226                         }
227 #endif
228
229                         if ( rc == 0 || ( rc == -1 && (
230                                 ( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART)
231                                         == LDAP_OPT_OFF )
232                                 || errno != EINTR )))
233                         {
234                                 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
235                                     LDAP_TIMEOUT);
236                                 return( rc );
237                         }
238
239                         if ( rc == -1 ) {
240                                 rc = -2;        /* select interrupted: loop */
241                         } else {
242                                 rc = -2;
243                                 for ( lc = ld->ld_conns; rc == -2 && lc != NULL;
244                                     lc = nextlc ) {
245                                         nextlc = lc->lconn_next;
246                                         if ( lc->lconn_status ==
247                                             LDAP_CONNST_CONNECTED &&
248                                             ldap_is_read_ready( ld,
249                                             lc->lconn_sb )) {
250                                                 rc = try_read1msg( ld, msgid, all,
251                                                     lc->lconn_sb, lc, result );
252                                         }
253                                 }
254                         }
255                 }
256 #endif /* !LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
257
258                 if ( rc == -2 && tvp != NULL ) {
259                         tmp_time = time( NULL );
260                         if (( tv.tv_sec -=  ( tmp_time - start_time )) <= 0 ) {
261                                 rc = 0; /* timed out */
262                                 ld->ld_errno = LDAP_TIMEOUT;
263                                 break;
264                         }
265
266                         Debug( LDAP_DEBUG_TRACE, "wait4msg:  %ld secs to go\n",
267                                (long) tv.tv_sec, 0, 0 );
268                         start_time = tmp_time;
269                 }
270         }
271
272         return( rc );
273 }
274
275
276 static int
277 try_read1msg( LDAP *ld, int msgid, int all, Sockbuf *sb,
278 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
279     LDAPConn *lc,
280 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
281     LDAPMessage **result )
282 {
283         BerElement      *ber;
284         LDAPMessage     *new, *l, *prev, *tmp;
285         long            id;
286         unsigned long   tag, len;
287         int             foundit = 0;
288 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
289         LDAPRequest     *lr;
290         BerElement      tmpber;
291         int             rc, refer_cnt, hadref, simple_request;
292         unsigned long   lderr;
293
294         assert( ld != NULL );
295         assert( lc != NULL );
296         
297         ber = &lc->lconn_ber;
298
299 #else
300         assert( ld != NULL );
301
302         ber = &ld->ld_ber;
303 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
304
305         Debug( LDAP_DEBUG_TRACE, "read1msg\n", 0, 0, 0 );
306
307 #if 0
308         ber_init_w_nullc( &ber, 0 );
309         ldap_set_ber_options( ld, &ber );
310 #endif
311         /* get the next message */
312         if ( (tag = ber_get_next( sb, &len, ber ))
313             != LDAP_TAG_MESSAGE ) {
314                 if ( tag == LBER_DEFAULT) {
315 #ifdef LDAP_DEBUG                  
316                         Debug( LDAP_DEBUG_CONNS,
317                               "ber_get_next failed.\n", 0, 0, 0 );
318 #endif             
319 #ifdef EWOULDBLOCK                      
320                         if (errno==EWOULDBLOCK) return -2;
321 #endif
322 #ifdef EAGAIN
323                         if (errno == EAGAIN) return -2;
324 #endif
325                         ld->ld_errno = LDAP_SERVER_DOWN;
326                         return -1;
327                 }
328                 ld->ld_errno = LDAP_LOCAL_ERROR;
329                 return -1;
330         }
331
332         /* message id */
333         if ( ber_get_int( ber, &id ) == LBER_ERROR ) {
334                 ld->ld_errno = LDAP_DECODING_ERROR;
335                 return( -1 );
336         }
337
338         /* if it's been abandoned, toss it */
339         if ( ldap_abandoned( ld, (int)id ) ) {
340                 ber_clear( ber, 1 );    /* gack! */
341                 return( -2 );   /* continue looking */
342         }
343
344 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
345         if (( lr = ldap_find_request_by_msgid( ld, id )) == NULL ) {
346                 Debug( LDAP_DEBUG_ANY,
347                     "no request for response with msgid %ld (tossing)\n",
348                     id, 0, 0 );
349                 ber_clear( ber, 1 );    /* gack! */
350                 return( -2 );   /* continue looking */
351         }
352         Debug( LDAP_DEBUG_TRACE, "ldap_read: %s msgid %ld, original id %d\n",
353             ( tag == LDAP_RES_SEARCH_ENTRY ) ? "entry" : 
354                 ( tag == LDAP_RES_SEARCH_REFERENCE ) ? "reference" : "result",
355                 id, lr->lr_origid );
356         id = lr->lr_origid;
357 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
358
359         /* the message type */
360         if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
361                 ld->ld_errno = LDAP_DECODING_ERROR;
362                 return( -1 );
363         }
364
365 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
366         refer_cnt = 0;
367         hadref = simple_request = 0;
368         rc = -2;        /* default is to keep looking (no response found) */
369         lr->lr_res_msgtype = tag;
370
371         if ( tag != LDAP_RES_SEARCH_ENTRY ) {
372                 if ( ld->ld_version >= LDAP_VERSION2 &&
373                         ( lr->lr_parent != NULL ||
374                         ( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS)
375                                 != LDAP_OPT_OFF ) ) )
376                 {
377                         tmpber = *ber;  /* struct copy */
378                         if ( ber_scanf( &tmpber, "{iaa}", &lderr,
379                             &lr->lr_res_matched, &lr->lr_res_error )
380                             != LBER_ERROR ) {
381                                 if ( lderr != LDAP_SUCCESS ) {
382                                         /* referrals are in error string */
383                                         refer_cnt = ldap_chase_referrals( ld, lr,
384                                             &lr->lr_res_error, &hadref );
385                                 }
386
387                                 /* save errno, message, and matched string */
388                                 if ( !hadref || lr->lr_res_error == NULL ) {
389                                         lr->lr_res_errno = ( lderr ==
390                                         LDAP_PARTIAL_RESULTS ) ? LDAP_SUCCESS
391                                         : lderr;
392                                 } else if ( ld->ld_errno != LDAP_SUCCESS ) {
393                                         lr->lr_res_errno = ld->ld_errno;
394                                 } else {
395                                         lr->lr_res_errno = LDAP_PARTIAL_RESULTS;
396                                 }
397 Debug( LDAP_DEBUG_TRACE,
398     "new result:  res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
399     lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
400     lr->lr_res_matched ? lr->lr_res_matched : "" );
401                         }
402                 }
403
404                 Debug( LDAP_DEBUG_TRACE,
405                     "read1msg:  %d new referrals\n", refer_cnt, 0, 0 );
406
407                 if ( refer_cnt != 0 ) { /* chasing referrals */
408                         ber_clear( ber, 1 );    /* gack! */
409                         if ( refer_cnt < 0 ) {
410                                 return( -1 );   /* fatal error */
411                         }
412                         lr->lr_status = LDAP_REQST_CHASINGREFS;
413                 } else {
414                         if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
415                                 /* request without any referrals */
416                                 simple_request = ( hadref ? 0 : 1 );
417                         } else {
418                                 /* request with referrals or child request */
419                                 ber_clear( ber, 1 );    /* gack! */
420                         }
421
422                         while ( lr->lr_parent != NULL ) {
423                                 merge_error_info( ld, lr->lr_parent, lr );
424
425                                 lr = lr->lr_parent;
426                                 if ( --lr->lr_outrefcnt > 0 ) {
427                                         break;  /* not completely done yet */
428                                 }
429                         }
430
431                         if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
432                                 id = lr->lr_msgid;
433                                 tag = lr->lr_res_msgtype;
434                                 Debug( LDAP_DEBUG_ANY, "request %ld done\n",
435                                     id, 0, 0 );
436 Debug( LDAP_DEBUG_TRACE,
437 "res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
438 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
439 lr->lr_res_matched ? lr->lr_res_matched : "" );
440                                 if ( !simple_request ) {
441                                         ber_clear( ber, 1 ); /* gack! */
442                                         if ( build_result_ber( ld, ber, lr )
443                                             == LBER_ERROR ) {
444                                                 ld->ld_errno = LDAP_NO_MEMORY;
445                                                 rc = -1; /* fatal error */
446                                         }
447                                 }
448
449                                 ldap_free_request( ld, lr );
450                         }
451
452                         if ( lc != NULL ) {
453                                 ldap_free_connection( ld, lc, 0, 1 );
454                         }
455                 }
456         }
457
458         if ( ber->ber_buf == NULL ) {
459                 return( rc );
460         }
461
462 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
463         /* make a new ldap message */
464         if ( (new = (LDAPMessage *) calloc( 1, sizeof(LDAPMessage) ))
465             == NULL ) {
466                 ld->ld_errno = LDAP_NO_MEMORY;
467                 return( -1 );
468         }
469         new->lm_msgid = (int)id;
470         new->lm_msgtype = tag;
471         new->lm_ber = ber_dup( ber );
472         ber_clear( ber, 0 ); /* don't kill buffer */
473
474 #ifndef LDAP_NOCACHE
475                 if ( ld->ld_cache != NULL ) {
476                         ldap_add_result_to_cache( ld, new );
477                 }
478 #endif /* LDAP_NOCACHE */
479
480         /* is this the one we're looking for? */
481         if ( msgid == LDAP_RES_ANY || id == msgid ) {
482                 if ( all == 0
483                     || (new->lm_msgtype != LDAP_RES_SEARCH_RESULT
484                     && new->lm_msgtype != LDAP_RES_SEARCH_ENTRY) ) {
485                         *result = new;
486                         ld->ld_errno = LDAP_SUCCESS;
487                         return( tag );
488                 } else if ( new->lm_msgtype == LDAP_RES_SEARCH_RESULT) {
489                         foundit = 1;    /* return the chain later */
490                 }
491         }
492
493         /* 
494          * if not, we must add it to the list of responses.  if
495          * the msgid is already there, it must be part of an existing
496          * search response.
497          */
498
499         prev = NULLMSG;
500         for ( l = ld->ld_responses; l != NULLMSG; l = l->lm_next ) {
501                 if ( l->lm_msgid == new->lm_msgid )
502                         break;
503                 prev = l;
504         }
505
506         /* not part of an existing search response */
507         if ( l == NULLMSG ) {
508                 if ( foundit ) {
509                         *result = new;
510                         ld->ld_errno = LDAP_SUCCESS;
511                         return( tag );
512                 }
513
514                 new->lm_next = ld->ld_responses;
515                 ld->ld_responses = new;
516                 return( -2 );   /* continue looking */
517         }
518
519         Debug( LDAP_DEBUG_TRACE, "adding response id %d type %d:\n",
520             new->lm_msgid, new->lm_msgtype, 0 );
521
522         /* part of a search response - add to end of list of entries */
523         for ( tmp = l; tmp->lm_chain != NULLMSG &&
524             tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY;
525             tmp = tmp->lm_chain )
526                 ;       /* NULL */
527         tmp->lm_chain = new;
528
529         /* return the whole chain if that's what we were looking for */
530         if ( foundit ) {
531                 if ( prev == NULLMSG )
532                         ld->ld_responses = l->lm_next;
533                 else
534                         prev->lm_next = l->lm_next;
535                 *result = l;
536                 ld->ld_errno = LDAP_SUCCESS;
537 #ifdef LDAP_WORLD_P16
538                 /*
539                  * XXX questionable fix; see text for [P16] on
540                  * http://www.critical-angle.com/ldapworld/patch/
541                  *
542                  * inclusion of this patch causes searchs to hang on
543                  * multiple platforms
544                  */
545                 return( l->lm_msgtype );
546 #else   /* LDAP_WORLD_P16 */
547                 return( tag );
548 #endif  /* !LDAP_WORLD_P16 */
549         }
550
551         return( -2 );   /* continue looking */
552 }
553
554
555 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
556 static unsigned long
557 build_result_ber( LDAP *ld, BerElement *ber, LDAPRequest *lr )
558 {
559         unsigned long   len;
560         long            along;
561
562         ber_init_w_nullc( ber, 0 );
563         ldap_set_ber_options( ld, ber );
564         if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
565             (unsigned long) lr->lr_res_msgtype, lr->lr_res_errno,
566             lr->lr_res_matched ? lr->lr_res_matched : "",
567             lr->lr_res_error ? lr->lr_res_error : "" ) == -1 ) {
568                 return( LBER_ERROR );
569         }
570
571         ber_reset( ber, 1 );
572         if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) {
573                 return( LBER_ERROR );
574         }
575
576         if ( ber_get_int( ber, &along ) == LBER_ERROR ) {
577                 return( LBER_ERROR );
578         }
579
580         return( ber_peek_tag( ber, &len ));
581 }
582
583
584 static void
585 merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
586 {
587 /*
588  * Merge error information in "lr" with "parentr" error code and string.
589  */
590         if ( lr->lr_res_errno == LDAP_PARTIAL_RESULTS ) {
591                 parentr->lr_res_errno = lr->lr_res_errno;
592                 if ( lr->lr_res_error != NULL ) {
593                         (void)ldap_append_referral( ld, &parentr->lr_res_error,
594                             lr->lr_res_error );
595                 }
596         } else if ( lr->lr_res_errno != LDAP_SUCCESS &&
597             parentr->lr_res_errno == LDAP_SUCCESS ) {
598                 parentr->lr_res_errno = lr->lr_res_errno;
599                 if ( parentr->lr_res_error != NULL ) {
600                         free( parentr->lr_res_error );
601                 }
602                 parentr->lr_res_error = lr->lr_res_error;
603                 lr->lr_res_error = NULL;
604                 if ( LDAP_NAME_ERROR( lr->lr_res_errno )) {
605                         if ( parentr->lr_res_matched != NULL ) {
606                                 free( parentr->lr_res_matched );
607                         }
608                         parentr->lr_res_matched = lr->lr_res_matched;
609                         lr->lr_res_matched = NULL;
610                 }
611         }
612
613         Debug( LDAP_DEBUG_TRACE, "merged parent (id %d) error info:  ",
614             parentr->lr_msgid, 0, 0 );
615         Debug( LDAP_DEBUG_TRACE, "result errno %d, error <%s>, matched <%s>\n",
616             parentr->lr_res_errno, parentr->lr_res_error ?
617             parentr->lr_res_error : "", parentr->lr_res_matched ?
618             parentr->lr_res_matched : "" );
619 }
620 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
621
622
623
624 #if defined( LDAP_CONNECTIONLESS ) || !defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS )
625
626 static int
627 ldap_select1( LDAP *ld, struct timeval *timeout )
628 {
629         fd_set          readfds;
630         static int      tblsize;
631
632         assert( ld != NULL );
633
634         if ( tblsize == 0 ) {
635 #ifdef HAVE_SYSCONF
636                 tblsize = sysconf( _SC_OPEN_MAX );
637 #elif HAVE_GETDTABLESIZE
638                 tblsize = getdtablesize();
639 #else
640                 tblsize = FD_SETSIZE;
641 #endif
642 #ifdef FD_SETSIZE
643                 if ( tblsize > FD_SETSIZE ) {
644                         tblsize = FD_SETSIZE;
645                 }
646 #endif  /* FD_SETSIZE */
647         }
648
649         FD_ZERO( &readfds );
650         FD_SET( ber_pvt_sb_get_desc(&ld->ld_sb), &readfds );
651
652         return( select( tblsize, &readfds, 0, 0, timeout ) );
653 }
654
655 #endif /* !LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
656
657
658 int
659 ldap_msgtype( LDAPMessage *lm )
660 {
661         assert( lm != NULL );
662         return ( lm == NULL ) ? lm->lm_msgtype : -1;
663 }
664
665
666 int
667 ldap_msgid( LDAPMessage *lm )
668 {
669         assert( lm != NULL );
670
671         return ( lm == NULL ) ? lm->lm_msgid : -1;
672 }
673
674
675 int
676 ldap_msgfree( LDAPMessage *lm )
677 {
678         LDAPMessage     *next;
679         int             type = 0;
680
681         assert( lm != NULL );
682
683         Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
684
685         for ( ; lm != NULLMSG; lm = next ) {
686                 next = lm->lm_chain;
687                 type = lm->lm_msgtype;
688                 ber_free( lm->lm_ber, 1 );
689                 free( (char *) lm );
690         }
691
692         return( type );
693 }
694
695 /*
696  * ldap_msgdelete - delete a message.  It returns:
697  *      0       if the entire message was deleted
698  *      -1      if the message was not found, or only part of it was found
699  */
700 int
701 ldap_msgdelete( LDAP *ld, int msgid )
702 {
703         LDAPMessage     *lm, *prev;
704
705         assert( ld != NULL );
706
707         Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete\n", 0, 0, 0 );
708
709         prev = NULLMSG;
710         for ( lm = ld->ld_responses; lm != NULLMSG; lm = lm->lm_next ) {
711                 if ( lm->lm_msgid == msgid )
712                         break;
713                 prev = lm;
714         }
715
716         if ( lm == NULLMSG )
717                 return( -1 );
718
719         if ( prev == NULLMSG )
720                 ld->ld_responses = lm->lm_next;
721         else
722                 prev->lm_next = lm->lm_next;
723
724         if ( ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY )
725                 return( -1 );
726
727         return( 0 );
728 }
729
730
731 /*
732  * return 1 if message msgid is waiting to be abandoned, 0 otherwise
733  */
734 static int
735 ldap_abandoned( LDAP *ld, int msgid )
736 {
737         int     i;
738
739         if ( ld->ld_abandoned == NULL )
740                 return( 0 );
741
742         for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
743                 if ( ld->ld_abandoned[i] == msgid )
744                         return( 1 );
745
746         return( 0 );
747 }
748
749
750 static int
751 ldap_mark_abandoned( LDAP *ld, int msgid )
752 {
753         int     i;
754
755         if ( ld->ld_abandoned == NULL )
756                 return( -1 );
757
758         for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
759                 if ( ld->ld_abandoned[i] == msgid )
760                         break;
761
762         if ( ld->ld_abandoned[i] == -1 )
763                 return( -1 );
764
765         for ( ; ld->ld_abandoned[i] != -1; i++ ) {
766                 ld->ld_abandoned[i] = ld->ld_abandoned[i + 1];
767         }
768
769         return( 0 );
770 }
771
772
773 #ifdef LDAP_CONNECTIONLESS
774 int
775 cldap_getmsg( LDAP *ld, struct timeval *timeout, BerElement *ber )
776 {
777         int             rc;
778         unsigned long   tag, len;
779
780         if ( ! ber_pvt_sb_data_ready(&ld->ld_sb) ) {
781                 rc = ldap_select1( ld, timeout );
782                 if ( rc == -1 || rc == 0 ) {
783                         ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
784                             LDAP_TIMEOUT);
785                         return( rc );
786                 }
787         }
788
789         /* get the next message */
790         if ( (tag = ber_get_next( &ld->ld_sb, &len, ber ))
791             != LDAP_TAG_MESSAGE ) {
792                 ld->ld_errno = (tag == LBER_DEFAULT ? LDAP_SERVER_DOWN :
793                     LDAP_LOCAL_ERROR);
794                 return( -1 );
795         }
796
797         return( tag );
798 }
799 #endif /* LDAP_CONNECTIONLESS */