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