2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 * Copyright (c) 1990 Regents of the University of Michigan.
9 * result.c - wait for an ldap result
18 #include <ac/socket.h>
19 #include <ac/string.h>
21 #include <ac/unistd.h>
26 static int ldap_abandoned LDAP_P(( LDAP *ld, int msgid ));
27 static int ldap_mark_abandoned LDAP_P(( LDAP *ld, int msgid ));
28 static int wait4msg LDAP_P(( LDAP *ld, int msgid, int all, struct timeval *timeout,
29 LDAPMessage **result ));
30 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
31 static int try_read1msg LDAP_P(( LDAP *ld, int msgid, int all, Sockbuf *sb, LDAPConn *lc,
32 LDAPMessage **result ));
33 static unsigned long build_result_ber LDAP_P(( LDAP *ld, BerElement *ber, LDAPRequest *lr ));
34 static void merge_error_info LDAP_P(( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr ));
35 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
36 static int try_read1msg LDAP_P(( LDAP *ld, int msgid, int all, Sockbuf *sb,
37 LDAPMessage **result ));
38 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
39 #if defined( LDAP_CONNECTIONLESS ) || !defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS )
40 static int ldap_select1 LDAP_P(( LDAP *ld, struct timeval *timeout ));
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.
56 * ldap_result( s, msgid, all, timeout, result )
59 ldap_result( LDAP *ld, int msgid, int all, struct timeval *timeout,
60 LDAPMessage **result )
62 LDAPMessage *lm, *lastlm, *nextlm;
65 assert( result != NULL );
67 Debug( LDAP_DEBUG_TRACE, "ldap_result\n", 0, 0, 0 );
73 if( result == NULL ) {
74 ld->ld_errno = LDAP_PARAM_ERROR;
79 * First, look through the list of responses we have received on
80 * this association and see if the response we're interested in
81 * is there. If it is, return it. If not, call wait4msg() to
82 * wait until it arrives or timeout occurs.
87 for ( lm = ld->ld_responses; lm != NULLMSG; lm = nextlm ) {
90 if ( ldap_abandoned( ld, lm->lm_msgid ) ) {
91 ldap_mark_abandoned( ld, lm->lm_msgid );
93 if ( lastlm == NULLMSG ) {
94 ld->ld_responses = lm->lm_next;
96 lastlm->lm_next = nextlm;
104 if ( msgid == LDAP_RES_ANY || lm->lm_msgid == msgid ) {
108 || (lm->lm_msgtype != LDAP_RES_SEARCH_RESULT
109 && lm->lm_msgtype != LDAP_RES_SEARCH_REFERENCE /* LDAPv3 */
110 && lm->lm_msgtype != LDAP_RES_SEARCH_ENTRY) )
113 for ( tmp = lm; tmp != NULLMSG; tmp = tmp->lm_chain ) {
114 if ( tmp->lm_msgtype == LDAP_RES_SEARCH_RESULT )
118 if ( tmp == NULLMSG ) {
119 return( wait4msg( ld, msgid, all, timeout,
127 if ( lm == NULLMSG ) {
128 return( wait4msg( ld, msgid, all, timeout, result ) );
131 if ( lastlm == NULLMSG ) {
132 ld->ld_responses = (all == 0 && lm->lm_chain != NULLMSG
133 ? lm->lm_chain : lm->lm_next);
135 lastlm->lm_next = (all == 0 && lm->lm_chain != NULLMSG
136 ? lm->lm_chain : lm->lm_next);
139 lm->lm_chain = NULLMSG;
140 lm->lm_next = NULLMSG;
143 ld->ld_errno = LDAP_SUCCESS;
144 return( lm->lm_msgtype );
148 wait4msg( LDAP *ld, int msgid, int all, struct timeval *timeout,
149 LDAPMessage **result )
152 struct timeval tv, *tvp;
153 time_t start_time = 0;
155 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
156 LDAPConn *lc, *nextlc;
157 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
159 assert( ld != NULL );
160 assert( result != NULL );
163 if ( timeout == NULL ) {
164 Debug( LDAP_DEBUG_TRACE, "wait4msg (infinite timeout)\n",
167 Debug( LDAP_DEBUG_TRACE, "wait4msg (timeout %ld sec, %ld usec)\n",
168 (long) timeout->tv_sec, (long) timeout->tv_usec, 0 );
170 #endif /* LDAP_DEBUG */
172 if ( timeout == NULL ) {
177 start_time = time( NULL );
182 #ifndef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
184 if ( ! ber_pvt_sb_data_ready(&ld->ld_sb) ) {
185 rc = ldap_select1( ld, tvp );
187 if ( rc == 0 || ( rc == -1 && (
188 ( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART)
190 || errno != EINTR ))) {
191 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
198 rc = -2; /* select interrupted: loop */
200 rc = try_read1msg( ld, msgid, all, &ld->ld_sb, result );
202 #else /* !LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
204 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
205 ldap_dump_connection( ld, ld->ld_conns, 1 );
206 ldap_dump_requests_and_responses( ld );
208 #endif /* LDAP_DEBUG */
209 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
210 if ( ber_pvt_sb_data_ready(lc->lconn_sb) ) {
211 rc = try_read1msg( ld, msgid, all, lc->lconn_sb,
218 rc = do_ldap_select( ld, tvp );
223 Debug( LDAP_DEBUG_TRACE,
224 "do_ldap_select returned -1: errno %d\n",
229 if ( rc == 0 || ( rc == -1 && (
230 ( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART)
232 || errno != EINTR )))
234 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
240 rc = -2; /* select interrupted: loop */
243 for ( lc = ld->ld_conns; rc == -2 && lc != NULL;
245 nextlc = lc->lconn_next;
246 if ( lc->lconn_status ==
247 LDAP_CONNST_CONNECTED &&
248 ldap_is_read_ready( ld,
250 rc = try_read1msg( ld, msgid, all,
251 lc->lconn_sb, lc, result );
256 #endif /* !LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
258 if ( rc == -2 && tvp != NULL ) {
259 tmp_time = time( NULL );
260 if (( tv.tv_sec -= ( tmp_time - start_time )) <= 0 ) {
261 rc = 0; /* timed out */
262 ld->ld_errno = LDAP_TIMEOUT;
266 Debug( LDAP_DEBUG_TRACE, "wait4msg: %ld secs to go\n",
267 (long) tv.tv_sec, 0, 0 );
268 start_time = tmp_time;
277 try_read1msg( LDAP *ld, int msgid, int all, Sockbuf *sb,
278 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
280 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
281 LDAPMessage **result )
284 LDAPMessage *new, *l, *prev, *tmp;
286 unsigned long tag, len;
288 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
291 int rc, refer_cnt, hadref, simple_request;
294 assert( ld != NULL );
295 assert( lc != NULL );
297 ber = &lc->lconn_ber;
300 assert( ld != NULL );
303 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
305 Debug( LDAP_DEBUG_TRACE, "read1msg\n", 0, 0, 0 );
308 ber_init_w_nullc( &ber, 0 );
309 ldap_set_ber_options( ld, &ber );
311 /* get the next message */
312 if ( (tag = ber_get_next( sb, &len, ber ))
313 != LDAP_TAG_MESSAGE ) {
314 if ( tag == LBER_DEFAULT) {
316 Debug( LDAP_DEBUG_CONNS,
317 "ber_get_next failed.\n", 0, 0, 0 );
320 if (errno==EWOULDBLOCK) return -2;
323 if (errno == EAGAIN) return -2;
325 ld->ld_errno = LDAP_SERVER_DOWN;
328 ld->ld_errno = LDAP_LOCAL_ERROR;
333 if ( ber_get_int( ber, &id ) == LBER_ERROR ) {
334 ld->ld_errno = LDAP_DECODING_ERROR;
338 /* if it's been abandoned, toss it */
339 if ( ldap_abandoned( ld, (int)id ) ) {
340 ber_clear( ber, 1 ); /* gack! */
341 return( -2 ); /* continue looking */
344 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
345 if (( lr = ldap_find_request_by_msgid( ld, id )) == NULL ) {
346 Debug( LDAP_DEBUG_ANY,
347 "no request for response with msgid %ld (tossing)\n",
349 ber_clear( ber, 1 ); /* gack! */
350 return( -2 ); /* continue looking */
352 Debug( LDAP_DEBUG_TRACE, "ldap_read: %s msgid %ld, original id %d\n",
353 ( tag == LDAP_RES_SEARCH_ENTRY ) ? "entry" :
354 ( tag == LDAP_RES_SEARCH_REFERENCE ) ? "reference" : "result",
357 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
359 /* the message type */
360 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
361 ld->ld_errno = LDAP_DECODING_ERROR;
365 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
367 hadref = simple_request = 0;
368 rc = -2; /* default is to keep looking (no response found) */
369 lr->lr_res_msgtype = tag;
371 if ( tag != LDAP_RES_SEARCH_ENTRY ) {
372 if ( ld->ld_version >= LDAP_VERSION2 &&
373 ( lr->lr_parent != NULL ||
374 ( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS)
375 != LDAP_OPT_OFF ) ) )
377 tmpber = *ber; /* struct copy */
378 if ( ber_scanf( &tmpber, "{iaa}", &lderr,
379 &lr->lr_res_matched, &lr->lr_res_error )
381 if ( lderr != LDAP_SUCCESS ) {
382 /* referrals are in error string */
383 refer_cnt = ldap_chase_referrals( ld, lr,
384 &lr->lr_res_error, &hadref );
387 /* save errno, message, and matched string */
388 if ( !hadref || lr->lr_res_error == NULL ) {
389 lr->lr_res_errno = ( lderr ==
390 LDAP_PARTIAL_RESULTS ) ? LDAP_SUCCESS
392 } else if ( ld->ld_errno != LDAP_SUCCESS ) {
393 lr->lr_res_errno = ld->ld_errno;
395 lr->lr_res_errno = LDAP_PARTIAL_RESULTS;
397 Debug( LDAP_DEBUG_TRACE,
398 "new result: res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
399 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
400 lr->lr_res_matched ? lr->lr_res_matched : "" );
404 Debug( LDAP_DEBUG_TRACE,
405 "read1msg: %d new referrals\n", refer_cnt, 0, 0 );
407 if ( refer_cnt != 0 ) { /* chasing referrals */
408 ber_clear( ber, 1 ); /* gack! */
409 if ( refer_cnt < 0 ) {
410 return( -1 ); /* fatal error */
412 lr->lr_status = LDAP_REQST_CHASINGREFS;
414 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
415 /* request without any referrals */
416 simple_request = ( hadref ? 0 : 1 );
418 /* request with referrals or child request */
419 ber_clear( ber, 1 ); /* gack! */
422 while ( lr->lr_parent != NULL ) {
423 merge_error_info( ld, lr->lr_parent, lr );
426 if ( --lr->lr_outrefcnt > 0 ) {
427 break; /* not completely done yet */
431 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
433 tag = lr->lr_res_msgtype;
434 Debug( LDAP_DEBUG_ANY, "request %ld done\n",
436 Debug( LDAP_DEBUG_TRACE,
437 "res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
438 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
439 lr->lr_res_matched ? lr->lr_res_matched : "" );
440 if ( !simple_request ) {
441 ber_clear( ber, 1 ); /* gack! */
442 if ( build_result_ber( ld, ber, lr )
444 ld->ld_errno = LDAP_NO_MEMORY;
445 rc = -1; /* fatal error */
449 ldap_free_request( ld, lr );
453 ldap_free_connection( ld, lc, 0, 1 );
458 if ( ber->ber_buf == NULL ) {
462 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
463 /* make a new ldap message */
464 if ( (new = (LDAPMessage *) calloc( 1, sizeof(LDAPMessage) ))
466 ld->ld_errno = LDAP_NO_MEMORY;
469 new->lm_msgid = (int)id;
470 new->lm_msgtype = tag;
471 new->lm_ber = ber_dup( ber );
472 ber_clear( ber, 0 ); /* don't kill buffer */
475 if ( ld->ld_cache != NULL ) {
476 ldap_add_result_to_cache( ld, new );
478 #endif /* LDAP_NOCACHE */
480 /* is this the one we're looking for? */
481 if ( msgid == LDAP_RES_ANY || id == msgid ) {
483 || (new->lm_msgtype != LDAP_RES_SEARCH_RESULT
484 && new->lm_msgtype != LDAP_RES_SEARCH_ENTRY) ) {
486 ld->ld_errno = LDAP_SUCCESS;
488 } else if ( new->lm_msgtype == LDAP_RES_SEARCH_RESULT) {
489 foundit = 1; /* return the chain later */
494 * if not, we must add it to the list of responses. if
495 * the msgid is already there, it must be part of an existing
500 for ( l = ld->ld_responses; l != NULLMSG; l = l->lm_next ) {
501 if ( l->lm_msgid == new->lm_msgid )
506 /* not part of an existing search response */
507 if ( l == NULLMSG ) {
510 ld->ld_errno = LDAP_SUCCESS;
514 new->lm_next = ld->ld_responses;
515 ld->ld_responses = new;
516 return( -2 ); /* continue looking */
519 Debug( LDAP_DEBUG_TRACE, "adding response id %d type %d:\n",
520 new->lm_msgid, new->lm_msgtype, 0 );
522 /* part of a search response - add to end of list of entries */
523 for ( tmp = l; tmp->lm_chain != NULLMSG &&
524 tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY;
525 tmp = tmp->lm_chain )
529 /* return the whole chain if that's what we were looking for */
531 if ( prev == NULLMSG )
532 ld->ld_responses = l->lm_next;
534 prev->lm_next = l->lm_next;
536 ld->ld_errno = LDAP_SUCCESS;
537 #ifdef LDAP_WORLD_P16
539 * XXX questionable fix; see text for [P16] on
540 * http://www.critical-angle.com/ldapworld/patch/
542 * inclusion of this patch causes searchs to hang on
545 return( l->lm_msgtype );
546 #else /* LDAP_WORLD_P16 */
548 #endif /* !LDAP_WORLD_P16 */
551 return( -2 ); /* continue looking */
555 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
557 build_result_ber( LDAP *ld, BerElement *ber, LDAPRequest *lr )
562 ber_init_w_nullc( ber, 0 );
563 ldap_set_ber_options( ld, ber );
564 if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
565 (unsigned long) lr->lr_res_msgtype, lr->lr_res_errno,
566 lr->lr_res_matched ? lr->lr_res_matched : "",
567 lr->lr_res_error ? lr->lr_res_error : "" ) == -1 ) {
568 return( LBER_ERROR );
572 if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) {
573 return( LBER_ERROR );
576 if ( ber_get_int( ber, &along ) == LBER_ERROR ) {
577 return( LBER_ERROR );
580 return( ber_peek_tag( ber, &len ));
585 merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
588 * Merge error information in "lr" with "parentr" error code and string.
590 if ( lr->lr_res_errno == LDAP_PARTIAL_RESULTS ) {
591 parentr->lr_res_errno = lr->lr_res_errno;
592 if ( lr->lr_res_error != NULL ) {
593 (void)ldap_append_referral( ld, &parentr->lr_res_error,
596 } else if ( lr->lr_res_errno != LDAP_SUCCESS &&
597 parentr->lr_res_errno == LDAP_SUCCESS ) {
598 parentr->lr_res_errno = lr->lr_res_errno;
599 if ( parentr->lr_res_error != NULL ) {
600 free( parentr->lr_res_error );
602 parentr->lr_res_error = lr->lr_res_error;
603 lr->lr_res_error = NULL;
604 if ( LDAP_NAME_ERROR( lr->lr_res_errno )) {
605 if ( parentr->lr_res_matched != NULL ) {
606 free( parentr->lr_res_matched );
608 parentr->lr_res_matched = lr->lr_res_matched;
609 lr->lr_res_matched = NULL;
613 Debug( LDAP_DEBUG_TRACE, "merged parent (id %d) error info: ",
614 parentr->lr_msgid, 0, 0 );
615 Debug( LDAP_DEBUG_TRACE, "result errno %d, error <%s>, matched <%s>\n",
616 parentr->lr_res_errno, parentr->lr_res_error ?
617 parentr->lr_res_error : "", parentr->lr_res_matched ?
618 parentr->lr_res_matched : "" );
620 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
624 #if defined( LDAP_CONNECTIONLESS ) || !defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS )
627 ldap_select1( LDAP *ld, struct timeval *timeout )
632 assert( ld != NULL );
634 if ( tblsize == 0 ) {
636 tblsize = sysconf( _SC_OPEN_MAX );
637 #elif HAVE_GETDTABLESIZE
638 tblsize = getdtablesize();
640 tblsize = FD_SETSIZE;
643 if ( tblsize > FD_SETSIZE ) {
644 tblsize = FD_SETSIZE;
646 #endif /* FD_SETSIZE */
650 FD_SET( ber_pvt_sb_get_desc(&ld->ld_sb), &readfds );
652 return( select( tblsize, &readfds, 0, 0, timeout ) );
655 #endif /* !LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
659 ldap_msgtype( LDAPMessage *lm )
661 assert( lm != NULL );
662 return ( lm == NULL ) ? lm->lm_msgtype : -1;
667 ldap_msgid( LDAPMessage *lm )
669 assert( lm != NULL );
671 return ( lm == NULL ) ? lm->lm_msgid : -1;
676 ldap_msgfree( LDAPMessage *lm )
681 assert( lm != NULL );
683 Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
685 for ( ; lm != NULLMSG; lm = next ) {
687 type = lm->lm_msgtype;
688 ber_free( lm->lm_ber, 1 );
696 * ldap_msgdelete - delete a message. It returns:
697 * 0 if the entire message was deleted
698 * -1 if the message was not found, or only part of it was found
701 ldap_msgdelete( LDAP *ld, int msgid )
703 LDAPMessage *lm, *prev;
705 assert( ld != NULL );
707 Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete\n", 0, 0, 0 );
710 for ( lm = ld->ld_responses; lm != NULLMSG; lm = lm->lm_next ) {
711 if ( lm->lm_msgid == msgid )
719 if ( prev == NULLMSG )
720 ld->ld_responses = lm->lm_next;
722 prev->lm_next = lm->lm_next;
724 if ( ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY )
732 * return 1 if message msgid is waiting to be abandoned, 0 otherwise
735 ldap_abandoned( LDAP *ld, int msgid )
739 if ( ld->ld_abandoned == NULL )
742 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
743 if ( ld->ld_abandoned[i] == msgid )
751 ldap_mark_abandoned( LDAP *ld, int msgid )
755 if ( ld->ld_abandoned == NULL )
758 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
759 if ( ld->ld_abandoned[i] == msgid )
762 if ( ld->ld_abandoned[i] == -1 )
765 for ( ; ld->ld_abandoned[i] != -1; i++ ) {
766 ld->ld_abandoned[i] = ld->ld_abandoned[i + 1];
773 #ifdef LDAP_CONNECTIONLESS
775 cldap_getmsg( LDAP *ld, struct timeval *timeout, BerElement *ber )
778 unsigned long tag, len;
780 if ( ! ber_pvt_sb_data_ready(&ld->ld_sb) ) {
781 rc = ldap_select1( ld, timeout );
782 if ( rc == -1 || rc == 0 ) {
783 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
789 /* get the next message */
790 if ( (tag = ber_get_next( &ld->ld_sb, &len, ber ))
791 != LDAP_TAG_MESSAGE ) {
792 ld->ld_errno = (tag == LBER_DEFAULT ? LDAP_SERVER_DOWN :
799 #endif /* LDAP_CONNECTIONLESS */