]> git.sur5r.net Git - openldap/blob - libraries/libldap/result.c
e17210f93d4bb0be6da020a5442a66016b9320fa
[openldap] / libraries / libldap / result.c
1 /* result.c - wait for an ldap result */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2005 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
17  * All rights reserved.
18  */
19 /* This notice applies to changes, created by or for Novell, Inc.,
20  * to preexisting works for which notices appear elsewhere in this file.
21  *
22  * Copyright (C) 1999, 2000 Novell, Inc. All Rights Reserved.
23  *
24  * THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND TREATIES.
25  * USE, MODIFICATION, AND REDISTRIBUTION OF THIS WORK IS SUBJECT TO VERSION
26  * 2.0.1 OF THE OPENLDAP PUBLIC LICENSE, A COPY OF WHICH IS AVAILABLE AT
27  * HTTP://WWW.OPENLDAP.ORG/LICENSE.HTML OR IN THE FILE "LICENSE" IN THE
28  * TOP-LEVEL DIRECTORY OF THE DISTRIBUTION. ANY USE OR EXPLOITATION OF THIS
29  * WORK OTHER THAN AS AUTHORIZED IN VERSION 2.0.1 OF THE OPENLDAP PUBLIC
30  * LICENSE, OR OTHER PRIOR WRITTEN CONSENT FROM NOVELL, COULD SUBJECT THE
31  * PERPETRATOR TO CRIMINAL AND CIVIL LIABILITY. 
32  *---
33  * Modification to OpenLDAP source by Novell, Inc.
34  * April 2000 sfs Add code to process V3 referrals and search results
35  *---
36  * Note: A verbatim copy of version 2.0.1 of the OpenLDAP Public License 
37  * can be found in the file "build/LICENSE-2.0.1" in this distribution
38  * of OpenLDAP Software.
39  */
40 /* Portions Copyright (C) The Internet Society (1997)
41  * ASN.1 fragments are from RFC 2251; see RFC for full legal notices.
42  */
43
44 /*
45  * LDAPv3 (RFC2251)
46  *      LDAPResult ::= SEQUENCE {
47  *              resultCode              ENUMERATED { ... },
48  *              matchedDN               LDAPDN,
49  *              errorMessage    LDAPString,
50  *              referral                Referral OPTIONAL
51  *      }
52  *      Referral ::= SEQUENCE OF LDAPURL        (one or more)
53  *      LDAPURL ::= LDAPString                          (limited to URL chars)
54  */
55
56 #include "portable.h"
57
58 #include <stdio.h>
59
60 #include <ac/stdlib.h>
61
62 #include <ac/errno.h>
63 #include <ac/socket.h>
64 #include <ac/string.h>
65 #include <ac/time.h>
66 #include <ac/unistd.h>
67
68 #include "ldap-int.h"
69 #include "ldap_log.h"
70
71 static int ldap_abandoned LDAP_P(( LDAP *ld, ber_int_t msgid ));
72 static int ldap_mark_abandoned LDAP_P(( LDAP *ld, ber_int_t msgid ));
73 static int wait4msg LDAP_P(( LDAP *ld, ber_int_t msgid, int all, struct timeval *timeout,
74         LDAPMessage **result ));
75 static ber_tag_t try_read1msg LDAP_P(( LDAP *ld, ber_int_t msgid,
76         int all, Sockbuf *sb, LDAPConn **lc, LDAPMessage **result ));
77 static ber_tag_t build_result_ber LDAP_P(( LDAP *ld, BerElement **bp, LDAPRequest *lr ));
78 static void merge_error_info LDAP_P(( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr ));
79 static LDAPMessage * chkResponseList LDAP_P(( LDAP *ld, int msgid, int all));
80
81
82 /*
83  * ldap_result - wait for an ldap result response to a message from the
84  * ldap server.  If msgid is LDAP_RES_ANY (-1), any message will be
85  * accepted.  If msgid is LDAP_RES_UNSOLICITED (0), any unsolicited
86  * message is accepted.  Otherwise ldap_result will wait for a response
87  * with msgid.  If all is LDAP_MSG_ONE (0) the first message with id
88  * msgid will be accepted, otherwise, ldap_result will wait for all
89  * responses with id msgid and then return a pointer to the entire list
90  * of messages.  In general, this is only useful for search responses,
91  * which can be of three message types (zero or more entries, zero or
92  * search references, followed by an ldap result).  An extension to
93  * LDAPv3 allows partial extended responses to be returned in response
94  * to any request.  The type of the first message received is returned.
95  * When waiting, any messages that have been abandoned are discarded.
96  *
97  * Example:
98  *      ldap_result( s, msgid, all, timeout, result )
99  */
100 int
101 ldap_result(
102         LDAP *ld,
103         int msgid,
104         int all,
105         struct timeval *timeout,
106         LDAPMessage **result )
107 {
108         LDAPMessage     *lm;
109         int     rc;
110
111         assert( ld != NULL );
112         assert( result != NULL );
113
114         Debug( LDAP_DEBUG_TRACE, "ldap_result ld %p msgid %d\n", (void *)ld, msgid, 0 );
115
116 #ifdef LDAP_R_COMPILE
117         ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
118 #endif
119         lm = chkResponseList(ld, msgid, all);
120
121         if ( lm == NULL ) {
122                 rc = wait4msg( ld, msgid, all, timeout, result );
123         } else {
124                 *result = lm;
125                 ld->ld_errno = LDAP_SUCCESS;
126                 rc = lm->lm_msgtype;
127         }
128 #ifdef LDAP_R_COMPILE
129         ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
130 #endif
131         return( rc );
132 }
133
134 static LDAPMessage *
135 chkResponseList(
136         LDAP *ld,
137         int msgid,
138         int all)
139 {
140         LDAPMessage     *lm, **lastlm, *nextlm;
141     /*
142          * Look through the list of responses we have received on
143          * this association and see if the response we're interested in
144          * is there.  If it is, return it.  If not, call wait4msg() to
145          * wait until it arrives or timeout occurs.
146          */
147
148         Debug( LDAP_DEBUG_TRACE,
149                 "ldap_chkResponseList ld %p msgid %d all %d\n",
150                 (void *)ld, msgid, all );
151         lastlm = &ld->ld_responses;
152         for ( lm = ld->ld_responses; lm != NULL; lm = nextlm ) {
153                 nextlm = lm->lm_next;
154
155                 if ( ldap_abandoned( ld, lm->lm_msgid ) ) {
156                         Debug( LDAP_DEBUG_TRACE,
157                                 "ldap_chkResponseList msg abandoned, msgid %d\n",
158                             msgid, 0, 0 );
159                         ldap_mark_abandoned( ld, lm->lm_msgid );
160
161                         /* Remove this entry from list */
162                         *lastlm = nextlm;
163
164                         ldap_msgfree( lm );
165
166                         continue;
167                 }
168
169                 if ( msgid == LDAP_RES_ANY || lm->lm_msgid == msgid ) {
170                         LDAPMessage     *tmp;
171
172                         if ( all == LDAP_MSG_ONE || all == LDAP_MSG_RECEIVED ||
173                                 msgid == LDAP_RES_UNSOLICITED ) {
174                                 break;
175                         }
176
177                         tmp = lm->lm_chain_tail;
178                         if ((tmp->lm_msgtype == LDAP_RES_SEARCH_ENTRY) ||
179                                 (tmp->lm_msgtype == LDAP_RES_SEARCH_REFERENCE) ||
180                                 (tmp->lm_msgtype == LDAP_RES_INTERMEDIATE)) {
181                                 tmp = NULL;
182                         }
183
184                         if ( tmp == NULL ) {
185                                 lm = NULL;
186                         }
187
188                         break;
189                 }
190                 lastlm = &lm->lm_next;
191         }
192
193     if ( lm != NULL ) {
194                 /* Found an entry, remove it from the list */
195             if ( all == LDAP_MSG_ONE && lm->lm_chain != NULL ) {
196                         *lastlm = lm->lm_chain;
197                         lm->lm_chain->lm_next = lm->lm_next;
198                         lm->lm_chain->lm_chain_tail = ( lm->lm_chain_tail != lm ) ? lm->lm_chain_tail : lm->lm_chain;
199                         lm->lm_chain = NULL;
200                         lm->lm_chain_tail = NULL;
201             } else {
202                         *lastlm = lm->lm_next;
203                 }
204             lm->lm_next = NULL;
205     }
206
207 #ifdef LDAP_DEBUG
208         if( lm == NULL) {
209                 Debug( LDAP_DEBUG_TRACE,
210                         "ldap_chkResponseList returns ld %p NULL\n", (void *)ld, 0, 0);
211         } else {
212                 Debug( LDAP_DEBUG_TRACE,
213                         "ldap_chkResponseList returns ld %p msgid %d, type 0x%02lu\n",
214                         (void *)ld, lm->lm_msgid, (unsigned long) lm->lm_msgtype);
215         }
216 #endif
217     return lm;
218 }
219
220 static int
221 wait4msg(
222         LDAP *ld,
223         ber_int_t msgid,
224         int all,
225         struct timeval *timeout,
226         LDAPMessage **result )
227 {
228         int             rc;
229         struct timeval  tv = { 0 },
230                         tv0 = { 0 },
231                         *tvp;
232         time_t          start_time = 0;
233         time_t          tmp_time;
234         LDAPConn        *lc, *nextlc;
235
236         assert( ld != NULL );
237         assert( result != NULL );
238
239 #ifdef LDAP_DEBUG
240         if ( timeout == NULL ) {
241                 Debug( LDAP_DEBUG_TRACE, "wait4msg ld %p msgid %d (infinite timeout)\n",
242                         (void *)ld, msgid, 0 );
243         } else {
244                 Debug( LDAP_DEBUG_TRACE, "wait4msg ld %p msgid %d (timeout %ld usec)\n",
245                         (void *)ld, msgid, (long)timeout->tv_sec * 1000000 + timeout->tv_usec );
246         }
247 #endif /* LDAP_DEBUG */
248
249         if ( timeout == NULL ) {
250                 tvp = NULL;
251         } else {
252                 tv0 = *timeout;
253                 tv = *timeout;
254                 tvp = &tv;
255                 start_time = time( NULL );
256         }
257                     
258         rc = -2;
259         while ( rc == -2 ) {
260 #ifdef LDAP_DEBUG
261                 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
262                         Debug( LDAP_DEBUG_TRACE, "wait4msg continue ld %p msgid %d all %d\n",
263                                 (void *)ld, msgid, all );
264                         ldap_dump_connection( ld, ld->ld_conns, 1 );
265                         ldap_dump_requests_and_responses( ld );
266                 }
267 #endif /* LDAP_DEBUG */
268
269                 if ( (*result = chkResponseList(ld, msgid, all)) != NULL ) {
270                         rc = (*result)->lm_msgtype;
271
272                 } else {
273                         int lc_ready = 0;
274
275 #ifdef LDAP_R_COMPILE
276                         ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
277 #endif
278                         for ( lc = ld->ld_conns; lc != NULL; lc = nextlc ) {
279                                 nextlc = lc->lconn_next;
280                                 if ( ber_sockbuf_ctrl( lc->lconn_sb,
281                                                 LBER_SB_OPT_DATA_READY, NULL ) ) {
282 #ifdef LDAP_R_COMPILE
283                                         ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
284 #endif
285                                         rc = try_read1msg( ld, msgid, all, lc->lconn_sb,
286                                                 &lc, result );
287 #ifdef LDAP_R_COMPILE
288                                         ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
289 #endif
290                                         lc_ready = 1;
291                                         break;
292                                 }
293                         }
294 #ifdef LDAP_R_COMPILE
295                         ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
296 #endif
297
298                         if ( !lc_ready ) {
299                                 rc = ldap_int_select( ld, tvp );
300 #ifdef LDAP_DEBUG
301                                 if ( rc == -1 ) {
302                                         Debug( LDAP_DEBUG_TRACE,
303                                                 "ldap_int_select returned -1: errno %d\n",
304                                                 errno, 0, 0 );
305                                 }
306 #endif
307
308                                 if ( rc == 0 || ( rc == -1 && (
309                                         !LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART)
310                                                 || errno != EINTR )))
311                                 {
312                                         ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
313                                                 LDAP_TIMEOUT);
314                                         return( rc );
315                                 }
316
317                                 if ( rc == -1 ) {
318                                         rc = -2;        /* select interrupted: loop */
319                                 } else {
320                                         rc = -2;
321 #ifdef LDAP_R_COMPILE
322                                         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
323 #endif
324                                         if ( ld->ld_requests &&
325                                                 ld->ld_requests->lr_status == LDAP_REQST_WRITING &&
326                                                 ldap_is_write_ready( ld,
327                                                         ld->ld_requests->lr_conn->lconn_sb ) )
328                                         {
329                                                 ldap_int_flush_request( ld, ld->ld_requests );
330                                         }
331 #ifdef LDAP_R_COMPILE
332                                         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
333                                         ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
334 #endif
335                                         for ( lc = ld->ld_conns; rc == -2 && lc != NULL;
336                                                 lc = nextlc )
337                                         {
338                                                 nextlc = lc->lconn_next;
339                                                 if ( lc->lconn_status == LDAP_CONNST_CONNECTED &&
340                                                         ldap_is_read_ready( ld, lc->lconn_sb ))
341                                                 {
342 #ifdef LDAP_R_COMPILE
343                                                         ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
344 #endif
345                                                         rc = try_read1msg( ld, msgid, all,
346                                                                 lc->lconn_sb, &lc, result );
347                                                                 if ( lc == NULL ) lc = nextlc;
348 #ifdef LDAP_R_COMPILE
349                                                         ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
350 #endif
351                                                 }
352                                         }
353 #ifdef LDAP_R_COMPILE
354                                         ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
355 #endif
356                                 }
357                         }
358                 }
359
360                 if ( rc == -2 && tvp != NULL ) {
361                         tmp_time = time( NULL );
362                         tv0.tv_sec -= ( tmp_time - start_time );
363                         if ( tv0.tv_sec <= 0 ) {
364                                 rc = 0; /* timed out */
365                                 ld->ld_errno = LDAP_TIMEOUT;
366                                 break;
367                         }
368                         tv.tv_sec = tv0.tv_sec;
369
370                         Debug( LDAP_DEBUG_TRACE, "wait4msg ld %p %ld secs to go\n",
371                                 (void *)ld, (long) tv.tv_sec, 0 );
372                         start_time = tmp_time;
373                 }
374         }
375
376         return( rc );
377 }
378
379
380 static ber_tag_t
381 try_read1msg(
382         LDAP *ld,
383         ber_int_t msgid,
384         int all,
385         Sockbuf *sb,
386         LDAPConn **lcp,
387         LDAPMessage **result )
388 {
389         BerElement      *ber;
390         LDAPMessage     *newmsg, *l, *prev;
391         ber_int_t       id;
392         ber_tag_t       tag;
393         ber_len_t       len;
394         int             foundit = 0;
395         LDAPRequest     *lr, *tmplr;
396         LDAPConn        *lc;
397         BerElement      tmpber;
398         int             rc, refer_cnt, hadref, simple_request;
399         ber_int_t       lderr;
400
401 #ifdef LDAP_CONNECTIONLESS
402         LDAPMessage     *tmp = NULL, *chain_head = NULL;
403         int             moremsgs = 0, isv2 = 0;
404 #endif
405
406         /*
407          * v3ref = flag for V3 referral / search reference
408          * 0 = not a ref, 1 = sucessfully chased ref, -1 = pass ref to application
409          */
410         enum {
411                 V3REF_NOREF     = 0,
412                 V3REF_SUCCESS   = 1,
413                 V3REF_TOAPP     = -1
414         }       v3ref;
415
416         assert( ld != NULL );
417         assert( lcp != NULL );
418         assert( *lcp != NULL );
419         
420         Debug( LDAP_DEBUG_TRACE, "read1msg: ld %p msgid %d all %d\n",
421                 (void *)ld, msgid, all );
422
423         lc = *lcp;
424
425 retry:
426         if ( lc->lconn_ber == NULL ) {
427                 lc->lconn_ber = ldap_alloc_ber_with_options(ld);
428
429                 if( lc->lconn_ber == NULL ) {
430                         return -1;
431                 }
432         }
433
434         ber = lc->lconn_ber;
435         assert( LBER_VALID (ber) );
436
437         /* get the next message */
438         errno = 0;
439 #ifdef LDAP_CONNECTIONLESS
440         if ( LDAP_IS_UDP(ld) ) {
441                 struct sockaddr from;
442                 ber_int_sb_read(sb, &from, sizeof(struct sockaddr));
443                 if (ld->ld_options.ldo_version == LDAP_VERSION2) isv2=1;
444         }
445 nextresp3:
446 #endif
447         tag = ber_get_next( sb, &len, ber );
448         if ( tag == LDAP_TAG_MESSAGE ) {
449                 /*
450                  * We read a complete message.
451                  * The connection should no longer need this ber.
452                  */
453                 lc->lconn_ber = NULL;
454         }
455         if ( tag != LDAP_TAG_MESSAGE ) {
456                 if ( tag == LBER_DEFAULT) {
457 #ifdef LDAP_DEBUG                  
458                         Debug( LDAP_DEBUG_CONNS,
459                                 "ber_get_next failed.\n", 0, 0, 0 );
460 #endif             
461 #ifdef EWOULDBLOCK                      
462                         if (errno==EWOULDBLOCK) return -2;
463 #endif
464 #ifdef EAGAIN
465                         if (errno == EAGAIN) return -2;
466 #endif
467                         ld->ld_errno = LDAP_SERVER_DOWN;
468                         return -1;
469                 }
470                 ld->ld_errno = LDAP_LOCAL_ERROR;
471                 return -1;
472         }
473
474         /* message id */
475         if ( ber_get_int( ber, &id ) == LBER_ERROR ) {
476                 ber_free( ber, 1 );
477                 ld->ld_errno = LDAP_DECODING_ERROR;
478                 return( -1 );
479         }
480
481         /* if it's been abandoned, toss it */
482         if ( ldap_abandoned( ld, id ) ) {
483                 Debug( LDAP_DEBUG_ANY, "abandoned ld %p msgid %ld\n",
484                         (void *)ld, (long) id, 0);
485 retry_ber:
486                 ber_free( ber, 1 );
487                 if ( ber_sockbuf_ctrl( sb, LBER_SB_OPT_DATA_READY, NULL ) ) {
488                         goto retry;
489                 }
490                 return( -2 );   /* continue looking */
491         }
492
493         lr = ldap_find_request_by_msgid( ld, id );
494         if ( lr == NULL ) {
495                 Debug( LDAP_DEBUG_ANY,
496                         "no request for response on ld %p msgid %ld (tossing)\n",
497                         (void *)ld, (long) id, 0 );
498                 goto retry_ber;
499         }
500 #ifdef LDAP_CONNECTIONLESS
501         if (LDAP_IS_UDP(ld) && isv2) {
502                 ber_scanf(ber, "x{");
503         }
504 nextresp2:
505 #endif
506         /* the message type */
507         if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
508                 ld->ld_errno = LDAP_DECODING_ERROR;
509                 ber_free( ber, 1 );
510                 return( -1 );
511         }
512
513         Debug( LDAP_DEBUG_TRACE,
514                 "read1msg: ld %p msgid %ld message type %s\n",
515                 (void *)ld, (long) lr->lr_msgid, ldap_int_msgtype2str( tag ));
516
517         id = lr->lr_origid;
518         refer_cnt = 0;
519         hadref = simple_request = 0;
520         rc = -2;        /* default is to keep looking (no response found) */
521         lr->lr_res_msgtype = tag;
522
523         /*
524          * This code figures out if we are going to chase a
525          * referral / search reference, or pass it back to the application
526          */
527         v3ref = V3REF_NOREF;    /* Assume not a V3 search reference/referral */
528         if( (tag != LDAP_RES_SEARCH_ENTRY) && (ld->ld_version > LDAP_VERSION2) ) {
529                 BerElement      tmpber = *ber;  /* struct copy */
530                 char **refs = NULL;
531
532                 if( tag == LDAP_RES_SEARCH_REFERENCE ) {
533                         /* This is a V3 search reference */
534                         /* Assume we do not chase the reference,
535                          * but pass it to application */
536                         v3ref = V3REF_TOAPP;
537                         if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ||
538                                         (lr->lr_parent != NULL) )
539                         {
540                                 /* Get the referral list */
541                                 if ( ber_scanf( &tmpber, "{v}", &refs ) == LBER_ERROR ) {
542                                         rc = LDAP_DECODING_ERROR;
543                                 } else {
544                                         /* Note: refs array is freed by ldap_chase_v3referrals */
545                                         refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
546                                             1, &lr->lr_res_error, &hadref );
547                                         if ( refer_cnt > 0 ) {
548                                                 /* sucessfully chased reference */
549                                                 /* If haven't got end search, set chasing referrals */
550                                                 if( lr->lr_status != LDAP_REQST_COMPLETED) {
551                                                         lr->lr_status = LDAP_REQST_CHASINGREFS;
552                                                         Debug( LDAP_DEBUG_TRACE,
553                                                                 "read1msg:  search ref chased, "
554                                                                 "mark request chasing refs, "
555                                                                 "id = %d\n",
556                                                                 lr->lr_msgid, 0, 0);
557                                                 }
558
559                                                 /* We sucessfully chased the reference */
560                                                 v3ref = V3REF_SUCCESS;
561                                         }
562                                 }
563                         }
564                 } else {
565                         /* Check for V3 referral */
566                         ber_len_t       len;
567                         char            *lr_res_error = NULL;
568
569 #ifdef LDAP_NULL_IS_NULL
570                         if ( ber_scanf( &tmpber, "{eAA",/*}*/ &lderr,
571                                     &lr->lr_res_matched, &lr_res_error )
572                                     != LBER_ERROR )
573 #else /* ! LDAP_NULL_IS_NULL */
574                         if ( ber_scanf( &tmpber, "{eaa",/*}*/ &lderr,
575                                     &lr->lr_res_matched, &lr_res_error )
576                                     != LBER_ERROR )
577 #endif /* ! LDAP_NULL_IS_NULL */
578                         {
579                                 if ( lr_res_error != NULL ) {
580 #ifndef LDAP_NULL_IS_NULL
581                                         if ( lr_res_error[ 0 ] == '\0' ) {
582                                                 LDAP_FREE( lr_res_error );
583                                                 lr_res_error = NULL;
584                                         } else
585 #endif /* ! LDAP_NULL_IS_NULL */
586                                         {
587                                                 if ( lr->lr_res_error != NULL ) {
588                                                         (void)ldap_append_referral( ld, &lr->lr_res_error, lr_res_error );
589                                                         LDAP_FREE( (char *)lr_res_error );
590
591                                                 } else {
592                                                         lr->lr_res_error = lr_res_error;
593                                                 }
594                                         }
595                                         lr_res_error = NULL;
596                                 }
597
598                                 /* Check if V3 referral */
599                                 if ( ber_peek_tag( &tmpber, &len ) == LDAP_TAG_REFERRAL ) {
600                                         /* We have a V3 referral, assume we cannot chase it */
601                                         v3ref = V3REF_TOAPP;
602                                         if( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS)
603                                                          || (lr->lr_parent != NULL) )
604                                         {
605                                                 /* Assume referral not chased and return it to app */
606                                                 v3ref = V3REF_TOAPP;
607
608                                                 /* Get the referral list */
609                                                 if( ber_scanf( &tmpber, "{v}", &refs) == LBER_ERROR) {
610                                                         rc = LDAP_DECODING_ERROR;
611                                                         lr->lr_status = LDAP_REQST_COMPLETED;
612                                                         Debug( LDAP_DEBUG_TRACE,
613                                                                 "read1msg: referral decode error, mark request completed, ld %p msgid %d\n",
614                                                                 (void *)ld, lr->lr_msgid, 0);
615                                                 } else {
616                                                         /* Chase the referral 
617                                                          * Note: refs arrary is freed by ldap_chase_v3referrals
618                                                          */
619                                                         refer_cnt = ldap_chase_v3referrals( ld, lr, refs,
620                                                             0, &lr->lr_res_error, &hadref );
621                                                         lr->lr_status = LDAP_REQST_COMPLETED;
622                                                         Debug( LDAP_DEBUG_TRACE,
623                                                                 "read1msg: referral chased, mark request completed, ld %p msgid %d\n",
624                                                                 (void *)ld, lr->lr_msgid, 0);
625                                                         if( refer_cnt > 0) {
626                                                                 /* Referral successfully chased */
627                                                                 v3ref = V3REF_SUCCESS;
628                                                         }
629                                                 }
630                                         }
631                                 }
632
633                                 if( lr->lr_res_matched != NULL ) {
634                                         LDAP_FREE( lr->lr_res_matched );
635                                         lr->lr_res_matched = NULL;
636                                 }
637                                 if( lr->lr_res_error != NULL ) {
638                                         LDAP_FREE( lr->lr_res_error );
639                                         lr->lr_res_error = NULL;
640                                 }
641                         }
642                 }
643         }
644
645         /* All results that just return a status, i.e. don't return data
646          * go through the following code.  This code also chases V2 referrals
647          * and checks if all referrals have been chased.
648          */
649         if ( (tag != LDAP_RES_SEARCH_ENTRY) && (v3ref != V3REF_TOAPP) &&
650                 (tag != LDAP_RES_INTERMEDIATE ))
651         {
652                 /* For a v3 search referral/reference, only come here if already chased it */
653                 if ( ld->ld_version >= LDAP_VERSION2 &&
654                         ( lr->lr_parent != NULL ||
655                         LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ) )
656                 {
657                         char            *lr_res_error = NULL;
658
659                         tmpber = *ber;  /* struct copy */
660                         if ( v3ref == V3REF_SUCCESS ) {
661                                 /* V3 search reference or V3 referral
662                                  * sucessfully chased. If this message
663                                  * is a search result, then it has no more
664                                  * outstanding referrals.
665                                  */
666                                 if ( tag == LDAP_RES_SEARCH_RESULT )
667                                         refer_cnt = 0;
668 #ifdef LDAP_NULL_IS_NULL
669                         } else if ( ber_scanf( &tmpber, "{eAA}", &lderr,
670                                 &lr->lr_res_matched, &lr_res_error )
671                                 != LBER_ERROR )
672 #else /* ! LDAP_NULL_IS_NULL */
673                         } else if ( ber_scanf( &tmpber, "{eaa}", &lderr,
674                                 &lr->lr_res_matched, &lr_res_error )
675                                 != LBER_ERROR )
676 #endif /* ! LDAP_NULL_IS_NULL */
677                         {
678                                 if ( lr_res_error != NULL ) {
679 #ifndef LDAP_NULL_IS_NULL
680                                         if ( lr_res_error[ 0 ] == '\0' ) {
681                                                 LDAP_FREE( lr_res_error );
682                                         } else
683 #endif /* ! LDAP_NULL_IS_NULL */
684                                         {
685                                                 if ( lr->lr_res_error != NULL ) {
686                                                         (void)ldap_append_referral( ld, &lr->lr_res_error, lr_res_error );
687                                                         LDAP_FREE( (char *)lr_res_error );
688                                                 } else {
689                                                         lr->lr_res_error = lr_res_error;
690                                                 }
691                                         }
692                                         lr_res_error = NULL;
693                                 }
694
695                                 switch ( lderr ) {
696                                 case LDAP_SUCCESS:
697                                 case LDAP_COMPARE_TRUE:
698                                 case LDAP_COMPARE_FALSE:
699                                         break;
700
701                                 default:
702                                         if ( lr->lr_res_error == NULL
703                                                 || lr->lr_res_error[ 0 ] == '\0' )
704                                         {
705                                                 break;
706                                         }
707
708                                         /* referrals are in error string */
709                                         refer_cnt = ldap_chase_referrals( ld, lr,
710                                                 &lr->lr_res_error, -1, &hadref );
711                                         lr->lr_status = LDAP_REQST_COMPLETED;
712                                         Debug( LDAP_DEBUG_TRACE,
713                                                 "read1msg:  V2 referral chased, "
714                                                 "mark request completed, id = %d\n",
715                                                 lr->lr_msgid, 0, 0 );
716                                         break;
717                                 }
718
719                                 /* save errno, message, and matched string */
720                                 if ( !hadref || lr->lr_res_error == NULL ) {
721                                         lr->lr_res_errno = ( lderr ==
722                                         LDAP_PARTIAL_RESULTS ) ? LDAP_SUCCESS
723                                         : lderr;
724                                 } else if ( ld->ld_errno != LDAP_SUCCESS ) {
725                                         lr->lr_res_errno = ld->ld_errno;
726                                 } else {
727                                         lr->lr_res_errno = LDAP_PARTIAL_RESULTS;
728                                 }
729
730                                 Debug( LDAP_DEBUG_TRACE, "new result:  "
731                                         "res_errno: %d, "
732                                         "res_error: <%s>, "
733                                         "res_matched: <%s>\n",
734                                         lr->lr_res_errno,
735                                         lr->lr_res_error ? lr->lr_res_error : "",
736                                         lr->lr_res_matched ? lr->lr_res_matched : "" );
737                         }
738
739                         /* in any case, don't leave any lr_res_error 'round */
740                         if ( lr_res_error ) {
741                                 LDAP_FREE( lr_res_error );
742                         }
743                 }
744
745                 Debug( LDAP_DEBUG_TRACE,
746                         "read1msg: ld %p %d new referrals\n",
747                         (void *)ld, refer_cnt, 0 );
748
749                 if ( refer_cnt != 0 ) { /* chasing referrals */
750                         ber_free( ber, 1 );
751                         ber = NULL;
752                         if ( refer_cnt < 0 ) {
753                                 return( -1 );   /* fatal error */
754                         }
755                         lr->lr_res_errno = LDAP_SUCCESS; /* sucessfully chased referral */
756                 } else {
757                         if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
758                                 /* request without any referrals */
759                                 simple_request = ( hadref ? 0 : 1 );
760                         } else {
761                                 /* request with referrals or child request */
762                                 ber_free( ber, 1 );
763                                 ber = NULL;
764                         }
765
766                         lr->lr_status = LDAP_REQST_COMPLETED; /* declare this request done */
767                         Debug( LDAP_DEBUG_TRACE,
768                                 "read1msg:  mark request completed, ld %p msgid %d\n",
769                                 (void *)ld, lr->lr_msgid, 0);
770                         while ( lr->lr_parent != NULL ) {
771                                 merge_error_info( ld, lr->lr_parent, lr );
772
773                                 lr = lr->lr_parent;
774                                 if ( --lr->lr_outrefcnt > 0 ) {
775                                         break;  /* not completely done yet */
776                                 }
777                         }
778
779                         /* Check if all requests are finished, lr is now parent */
780                         tmplr = lr;
781                         if (tmplr->lr_status == LDAP_REQST_COMPLETED) {
782                                 for ( tmplr=lr->lr_child;
783                                         tmplr != NULL;
784                                         tmplr=tmplr->lr_refnext)
785                                 {
786                                         if( tmplr->lr_status != LDAP_REQST_COMPLETED) break;
787                                 }
788                         }
789
790                         /* This is the parent request if the request has referrals */
791                         if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL &&
792                                 tmplr == NULL )
793                         {
794                                 id = lr->lr_msgid;
795                                 tag = lr->lr_res_msgtype;
796                                 Debug( LDAP_DEBUG_ANY, "request done: ld %p msgid %ld\n",
797                                         (void *)ld, (long) id, 0 );
798 Debug( LDAP_DEBUG_TRACE,
799 "res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
800 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
801 lr->lr_res_matched ? lr->lr_res_matched : "" );
802                                 if ( !simple_request ) {
803                                         ber_free( ber, 1 );
804                                         ber = NULL;
805                                         if ( build_result_ber( ld, &ber, lr )
806                                             == LBER_ERROR ) {
807                                                 rc = -1; /* fatal error */
808                                         }
809                                 }
810
811 #ifdef LDAP_R_COMPILE
812                                 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
813 #endif
814                                 ldap_free_request( ld, lr );
815 #ifdef LDAP_R_COMPILE
816                                 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
817 #endif
818                         }
819
820                         if ( lc != NULL ) {
821 #ifdef LDAP_R_COMPILE
822                                 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
823 #endif
824                                 ldap_free_connection( ld, lc, 0, 1 );
825 #ifdef LDAP_R_COMPILE
826                                 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
827 #endif
828                                 *lcp = NULL;
829                         }
830                 }
831         }
832
833         if ( ber == NULL ) {
834                 return( rc );
835         }
836
837         /* make a new ldap message */
838         newmsg = (LDAPMessage *) LDAP_CALLOC( 1, sizeof(LDAPMessage) );
839         if ( newmsg == NULL ) {
840                 ld->ld_errno = LDAP_NO_MEMORY;
841                 return( -1 );
842         }
843         newmsg->lm_msgid = (int)id;
844         newmsg->lm_msgtype = tag;
845         newmsg->lm_ber = ber;
846         newmsg->lm_chain_tail = newmsg;
847
848 #ifdef LDAP_CONNECTIONLESS
849         /* CLDAP replies all fit in a single datagram. In LDAPv2 RFC1798
850          * the responses are all a sequence wrapped in one message. In
851          * LDAPv3 each response is in its own message. The datagram must
852          * end with a SearchResult. We can't just parse each response in
853          * separate calls to try_read1msg because the header info is only
854          * present at the beginning of the datagram, not at the beginning
855          * of each response. So parse all the responses at once and queue
856          * them up, then pull off the first response to return to the
857          * caller when all parsing is complete.
858          */
859         if ( LDAP_IS_UDP(ld) ) {
860                 /* If not a result, look for more */
861                 if ( tag != LDAP_RES_SEARCH_RESULT ) {
862                         int ok = 0;
863                         moremsgs = 1;
864                         if (isv2) {
865                                 /* LDAPv2: dup the current ber, skip past the current
866                                  * response, and see if there are any more after it.
867                                  */
868                                 ber = ber_dup( ber );
869                                 ber_scanf( ber, "x" );
870                                 if (ber_peek_tag(ber, &len) != LBER_DEFAULT) {
871                                         /* There's more - dup the ber buffer so they can all be
872                                          * individually freed by ldap_msgfree.
873                                          */
874                                         struct berval bv;
875                                         ber_get_option(ber, LBER_OPT_BER_REMAINING_BYTES, &len);
876                                         bv.bv_val = LDAP_MALLOC(len);
877                                         if (bv.bv_val) {
878                                                 ok=1;
879                                                 ber_read(ber, bv.bv_val, len);
880                                                 bv.bv_len = len;
881                                                 ber_init2(ber, &bv, ld->ld_lberoptions );
882                                         }
883                                 }
884                         } else {
885                                 /* LDAPv3: Just allocate a new ber. Since this is a buffered
886                                  * datagram, if the sockbuf is readable we still have data
887                                  * to parse.
888                                  */
889                                 ber = ldap_alloc_ber_with_options(ld);
890                                 if (ber_sockbuf_ctrl(sb, LBER_SB_OPT_DATA_READY, NULL)) ok=1;
891                         }
892                         /* set up response chain */
893                         if ( tmp == NULL ) {
894                                 newmsg->lm_next = ld->ld_responses;
895                                 ld->ld_responses = newmsg;
896                                 chain_head = newmsg;
897                         } else {
898                                 tmp->lm_chain = newmsg;
899                         }
900                         chain_head->lm_chain_tail = newmsg;
901                         tmp = newmsg;
902                         /* "ok" means there's more to parse */
903                         if (ok) {
904                                 if (isv2) goto nextresp2;
905                                 else goto nextresp3;
906                         } else {
907                                 /* got to end of datagram without a SearchResult. Free
908                                  * our dup'd ber, but leave any buffer alone. For v2 case,
909                                  * the previous response is still using this buffer. For v3,
910                                  * the new ber has no buffer to free yet.
911                                  */
912                                 ber_free(ber, 0);
913                                 return -1;
914                         }
915                 } else if ( moremsgs ) {
916                 /* got search result, and we had multiple responses in 1 datagram.
917                  * stick the result onto the end of the chain, and then pull the
918                  * first response off the head of the chain.
919                  */
920                         tmp->lm_chain = newmsg;
921                         chain_head->lm_chain_tail = newmsg;
922                         *result = chkResponseList( ld, msgid, all );
923                         ld->ld_errno = LDAP_SUCCESS;
924                         return( (*result)->lm_msgtype );
925                 }
926         }
927 #endif /* LDAP_CONNECTIONLESS */
928
929         /* is this the one we're looking for? */
930         if ( msgid == LDAP_RES_ANY || id == msgid ) {
931                 if ( all == LDAP_MSG_ONE
932                     || (newmsg->lm_msgtype != LDAP_RES_SEARCH_RESULT
933                     && newmsg->lm_msgtype != LDAP_RES_SEARCH_ENTRY
934                     && newmsg->lm_msgtype != LDAP_RES_SEARCH_REFERENCE) ) {
935                         *result = newmsg;
936                         ld->ld_errno = LDAP_SUCCESS;
937                         return( tag );
938                 } else if ( newmsg->lm_msgtype == LDAP_RES_SEARCH_RESULT) {
939                         foundit = 1;    /* return the chain later */
940                 }
941         }
942
943         /* 
944          * if not, we must add it to the list of responses.  if
945          * the msgid is already there, it must be part of an existing
946          * search response.
947          */
948
949         prev = NULL;
950         for ( l = ld->ld_responses; l != NULL; l = l->lm_next ) {
951                 if ( l->lm_msgid == newmsg->lm_msgid )
952                         break;
953                 prev = l;
954         }
955
956         /* not part of an existing search response */
957         if ( l == NULL ) {
958                 if ( foundit ) {
959                         *result = newmsg;
960                         goto exit;
961                 }
962
963                 newmsg->lm_next = ld->ld_responses;
964                 ld->ld_responses = newmsg;
965                 goto exit;
966         }
967
968         Debug( LDAP_DEBUG_TRACE, "adding response ld %p msgid %ld type %ld:\n",
969                 (void *)ld, (long) newmsg->lm_msgid, (long) newmsg->lm_msgtype );
970
971         /* part of a search response - add to end of list of entries */
972         l->lm_chain_tail->lm_chain = newmsg;
973         l->lm_chain_tail = newmsg;
974
975         /* return the whole chain if that's what we were looking for */
976         if ( foundit ) {
977                 if ( prev == NULL )
978                         ld->ld_responses = l->lm_next;
979                 else
980                         prev->lm_next = l->lm_next;
981                 *result = l;
982         }
983
984 exit:
985         if ( foundit ) {
986                 ld->ld_errno = LDAP_SUCCESS;
987                 return( tag );
988         }
989         if ( ber_sockbuf_ctrl( sb, LBER_SB_OPT_DATA_READY, NULL ) ) {
990                 goto retry;
991         }
992         return( -2 );   /* continue looking */
993 }
994
995
996 static ber_tag_t
997 build_result_ber( LDAP *ld, BerElement **bp, LDAPRequest *lr )
998 {
999         ber_len_t       len;
1000         ber_tag_t       tag;
1001         ber_int_t       along;
1002         BerElement *ber;
1003
1004         *bp = NULL;
1005         ber = ldap_alloc_ber_with_options( ld );
1006
1007         if( ber == NULL ) {
1008                 ld->ld_errno = LDAP_NO_MEMORY;
1009                 return LBER_ERROR;
1010         }
1011
1012         if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
1013                 lr->lr_res_msgtype, lr->lr_res_errno,
1014                 lr->lr_res_matched ? lr->lr_res_matched : "",
1015                 lr->lr_res_error ? lr->lr_res_error : "" ) == -1 )
1016         {
1017                 ld->ld_errno = LDAP_ENCODING_ERROR;
1018                 ber_free(ber, 1);
1019                 return( LBER_ERROR );
1020         }
1021
1022         ber_reset( ber, 1 );
1023
1024         if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) {
1025                 ld->ld_errno = LDAP_DECODING_ERROR;
1026                 ber_free(ber, 1);
1027                 return( LBER_ERROR );
1028         }
1029
1030         if ( ber_get_enum( ber, &along ) == LBER_ERROR ) {
1031                 ld->ld_errno = LDAP_DECODING_ERROR;
1032                 ber_free(ber, 1);
1033                 return( LBER_ERROR );
1034         }
1035
1036         tag = ber_peek_tag( ber, &len );
1037
1038         if ( tag == LBER_ERROR ) {
1039                 ld->ld_errno = LDAP_DECODING_ERROR;
1040                 ber_free(ber, 1);
1041                 return( LBER_ERROR );
1042         }
1043
1044         *bp = ber;
1045         return tag;
1046 }
1047
1048
1049 static void
1050 merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
1051 {
1052 /*
1053  * Merge error information in "lr" with "parentr" error code and string.
1054  */
1055         if ( lr->lr_res_errno == LDAP_PARTIAL_RESULTS ) {
1056                 parentr->lr_res_errno = lr->lr_res_errno;
1057                 if ( lr->lr_res_error != NULL ) {
1058                         (void)ldap_append_referral( ld, &parentr->lr_res_error,
1059                             lr->lr_res_error );
1060                 }
1061         } else if ( lr->lr_res_errno != LDAP_SUCCESS &&
1062                 parentr->lr_res_errno == LDAP_SUCCESS )
1063         {
1064                 parentr->lr_res_errno = lr->lr_res_errno;
1065                 if ( parentr->lr_res_error != NULL ) {
1066                         LDAP_FREE( parentr->lr_res_error );
1067                 }
1068                 parentr->lr_res_error = lr->lr_res_error;
1069                 lr->lr_res_error = NULL;
1070                 if ( LDAP_NAME_ERROR( lr->lr_res_errno ) ) {
1071                         if ( parentr->lr_res_matched != NULL ) {
1072                                 LDAP_FREE( parentr->lr_res_matched );
1073                         }
1074                         parentr->lr_res_matched = lr->lr_res_matched;
1075                         lr->lr_res_matched = NULL;
1076                 }
1077         }
1078
1079         Debug( LDAP_DEBUG_TRACE, "merged parent (id %d) error info:  ",
1080             parentr->lr_msgid, 0, 0 );
1081         Debug( LDAP_DEBUG_TRACE, "result errno %d, error <%s>, matched <%s>\n",
1082             parentr->lr_res_errno, parentr->lr_res_error ?
1083             parentr->lr_res_error : "", parentr->lr_res_matched ?
1084             parentr->lr_res_matched : "" );
1085 }
1086
1087
1088
1089 int
1090 ldap_msgtype( LDAPMessage *lm )
1091 {
1092         assert( lm != NULL );
1093         return ( lm != NULL ) ? (int)lm->lm_msgtype : -1;
1094 }
1095
1096
1097 int
1098 ldap_msgid( LDAPMessage *lm )
1099 {
1100         assert( lm != NULL );
1101
1102         return ( lm != NULL ) ? lm->lm_msgid : -1;
1103 }
1104
1105
1106 char * ldap_int_msgtype2str( ber_tag_t tag )
1107 {
1108         switch( tag ) {
1109         case LDAP_RES_ADD: return "add";
1110         case LDAP_RES_BIND: return "bind";
1111         case LDAP_RES_COMPARE: return "compare";
1112         case LDAP_RES_DELETE: return "delete";
1113         case LDAP_RES_EXTENDED: return "extended-result";
1114         case LDAP_RES_INTERMEDIATE: return "intermediate";
1115         case LDAP_RES_MODIFY: return "modify";
1116         case LDAP_RES_RENAME: return "rename";
1117         case LDAP_RES_SEARCH_ENTRY: return "search-entry";
1118         case LDAP_RES_SEARCH_REFERENCE: return "search-reference";
1119         case LDAP_RES_SEARCH_RESULT: return "search-result";
1120         }
1121         return "unknown";
1122 }
1123
1124 int
1125 ldap_msgfree( LDAPMessage *lm )
1126 {
1127         LDAPMessage     *next;
1128         int             type = 0;
1129
1130         Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
1131
1132         for ( ; lm != NULL; lm = next ) {
1133                 next = lm->lm_chain;
1134                 type = lm->lm_msgtype;
1135                 ber_free( lm->lm_ber, 1 );
1136                 LDAP_FREE( (char *) lm );
1137         }
1138
1139         return( type );
1140 }
1141
1142 /*
1143  * ldap_msgdelete - delete a message.  It returns:
1144  *      0       if the entire message was deleted
1145  *      -1      if the message was not found, or only part of it was found
1146  */
1147 int
1148 ldap_msgdelete( LDAP *ld, int msgid )
1149 {
1150         LDAPMessage     *lm, *prev;
1151         int rc = 0;
1152
1153         assert( ld != NULL );
1154
1155         Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete\n", 0, 0, 0 );
1156
1157         prev = NULL;
1158 #ifdef LDAP_R_COMPILE
1159         ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
1160 #endif
1161         for ( lm = ld->ld_responses; lm != NULL; lm = lm->lm_next ) {
1162                 if ( lm->lm_msgid == msgid )
1163                         break;
1164                 prev = lm;
1165         }
1166
1167         if ( lm == NULL ) {
1168                 rc = -1;
1169         } else {
1170                 if ( prev == NULL )
1171                         ld->ld_responses = lm->lm_next;
1172                 else
1173                         prev->lm_next = lm->lm_next;
1174         }
1175 #ifdef LDAP_R_COMPILE
1176         ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
1177 #endif
1178         if ( lm && ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY )
1179                 rc = -1;
1180
1181         return( rc );
1182 }
1183
1184
1185 /*
1186  * return 1 if message msgid is waiting to be abandoned, 0 otherwise
1187  */
1188 static int
1189 ldap_abandoned( LDAP *ld, ber_int_t msgid )
1190 {
1191         int     i;
1192
1193         if ( ld->ld_abandoned == NULL )
1194                 return( 0 );
1195
1196         for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
1197                 if ( ld->ld_abandoned[i] == msgid )
1198                         return( 1 );
1199
1200         return( 0 );
1201 }
1202
1203
1204 static int
1205 ldap_mark_abandoned( LDAP *ld, ber_int_t msgid )
1206 {
1207         int     i;
1208
1209         if ( ld->ld_abandoned == NULL )
1210                 return( -1 );
1211
1212         for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
1213                 if ( ld->ld_abandoned[i] == msgid )
1214                         break;
1215
1216         if ( ld->ld_abandoned[i] == -1 )
1217                 return( -1 );
1218
1219         for ( ; ld->ld_abandoned[i] != -1; i++ ) {
1220                 ld->ld_abandoned[i] = ld->ld_abandoned[i + 1];
1221         }
1222
1223         return( 0 );
1224 }