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