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