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
14 * LDAPResult ::= SEQUENCE {
15 * resultCode ENUMERATED { ... },
17 * errorMessage LDAPString,
18 * referral Referral OPTIONAL
20 * Referral ::= SEQUENCE OF LDAPURL (one or more)
21 * LDAPURL ::= LDAPString (limited to URL chars)
28 #include <ac/stdlib.h>
31 #include <ac/socket.h>
32 #include <ac/string.h>
34 #include <ac/unistd.h>
39 static int ldap_abandoned LDAP_P(( LDAP *ld, int msgid ));
40 static int ldap_mark_abandoned LDAP_P(( LDAP *ld, int msgid ));
41 static int wait4msg LDAP_P(( LDAP *ld, int msgid, int all, struct timeval *timeout,
42 LDAPMessage **result ));
43 static int try_read1msg LDAP_P(( LDAP *ld, int msgid, int all, Sockbuf *sb, LDAPConn *lc,
44 LDAPMessage **result ));
45 static unsigned long build_result_ber LDAP_P(( LDAP *ld, BerElement *ber, LDAPRequest *lr ));
46 static void merge_error_info LDAP_P(( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr ));
50 * ldap_result - wait for an ldap result response to a message from the
51 * ldap server. If msgid is -1, any message will be accepted, otherwise
52 * ldap_result will wait for a response with msgid. If all is 0 the
53 * first message with id msgid will be accepted, otherwise, ldap_result
54 * will wait for all responses with id msgid and then return a pointer to
55 * the entire list of messages. This is only useful for search responses,
56 * which can be of two message types (zero or more entries, followed by an
57 * ldap result). The type of the first message received is returned.
58 * When waiting, any messages that have been abandoned are discarded.
61 * ldap_result( s, msgid, all, timeout, result )
64 ldap_result( LDAP *ld, int msgid, int all, struct timeval *timeout,
65 LDAPMessage **result )
67 LDAPMessage *lm, *lastlm, *nextlm;
70 assert( result != NULL );
72 Debug( LDAP_DEBUG_TRACE, "ldap_result\n", 0, 0, 0 );
78 if( result == NULL ) {
79 ld->ld_errno = LDAP_PARAM_ERROR;
84 * First, look through the list of responses we have received on
85 * this association and see if the response we're interested in
86 * is there. If it is, return it. If not, call wait4msg() to
87 * wait until it arrives or timeout occurs.
92 for ( lm = ld->ld_responses; lm != NULLMSG; lm = nextlm ) {
95 if ( ldap_abandoned( ld, lm->lm_msgid ) ) {
96 ldap_mark_abandoned( ld, lm->lm_msgid );
98 if ( lastlm == NULLMSG ) {
99 ld->ld_responses = lm->lm_next;
101 lastlm->lm_next = nextlm;
109 if ( msgid == LDAP_RES_ANY || lm->lm_msgid == msgid ) {
113 || (lm->lm_msgtype != LDAP_RES_SEARCH_RESULT
114 && lm->lm_msgtype != LDAP_RES_SEARCH_REFERENCE /* LDAPv3 */
115 && lm->lm_msgtype != LDAP_RES_SEARCH_ENTRY) )
118 for ( tmp = lm; tmp != NULLMSG; tmp = tmp->lm_chain ) {
119 if ( tmp->lm_msgtype == LDAP_RES_SEARCH_RESULT )
123 if ( tmp == NULLMSG ) {
124 return( wait4msg( ld, msgid, all, timeout,
132 if ( lm == NULLMSG ) {
133 return( wait4msg( ld, msgid, all, timeout, result ) );
136 if ( lastlm == NULLMSG ) {
137 ld->ld_responses = (all == 0 && lm->lm_chain != NULLMSG
138 ? lm->lm_chain : lm->lm_next);
140 lastlm->lm_next = (all == 0 && lm->lm_chain != NULLMSG
141 ? lm->lm_chain : lm->lm_next);
144 lm->lm_chain = NULLMSG;
145 lm->lm_next = NULLMSG;
148 ld->ld_errno = LDAP_SUCCESS;
149 return( lm->lm_msgtype );
153 wait4msg( LDAP *ld, int msgid, int all, struct timeval *timeout,
154 LDAPMessage **result )
157 struct timeval tv, *tvp;
158 time_t start_time = 0;
160 LDAPConn *lc, *nextlc;
162 assert( ld != NULL );
163 assert( result != NULL );
166 if ( timeout == NULL ) {
167 Debug( LDAP_DEBUG_TRACE, "wait4msg (infinite timeout)\n",
170 Debug( LDAP_DEBUG_TRACE, "wait4msg (timeout %ld sec, %ld usec)\n",
171 (long) timeout->tv_sec, (long) timeout->tv_usec, 0 );
173 #endif /* LDAP_DEBUG */
175 if ( timeout == NULL ) {
180 start_time = time( NULL );
186 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
187 ldap_dump_connection( ld, ld->ld_conns, 1 );
188 ldap_dump_requests_and_responses( ld );
190 #endif /* LDAP_DEBUG */
191 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
192 if ( ber_pvt_sb_data_ready(lc->lconn_sb) ) {
193 rc = try_read1msg( ld, msgid, all, lc->lconn_sb,
200 rc = do_ldap_select( ld, tvp );
205 Debug( LDAP_DEBUG_TRACE,
206 "do_ldap_select returned -1: errno %d\n",
211 if ( rc == 0 || ( rc == -1 && (
212 !LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART)
213 || errno != EINTR )))
215 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
221 rc = -2; /* select interrupted: loop */
224 for ( lc = ld->ld_conns; rc == -2 && lc != NULL;
226 nextlc = lc->lconn_next;
227 if ( lc->lconn_status ==
228 LDAP_CONNST_CONNECTED &&
229 ldap_is_read_ready( ld,
231 rc = try_read1msg( ld, msgid, all,
232 lc->lconn_sb, lc, result );
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;
246 Debug( LDAP_DEBUG_TRACE, "wait4msg: %ld secs to go\n",
247 (long) tv.tv_sec, 0, 0 );
248 start_time = tmp_time;
257 try_read1msg( LDAP *ld, int msgid, int all, Sockbuf *sb,
258 LDAPConn *lc, LDAPMessage **result )
261 LDAPMessage *new, *l, *prev, *tmp;
263 unsigned long tag, len;
267 int rc, refer_cnt, hadref, simple_request;
270 assert( ld != NULL );
271 assert( lc != NULL );
273 Debug( LDAP_DEBUG_TRACE, "read1msg\n", 0, 0, 0 );
275 if ( lc->lconn_ber == NULLBER ) {
276 lc->lconn_ber = ldap_alloc_ber_with_options(ld);
278 if( lc->lconn_ber == NULL ) {
284 assert( BER_VALID (ber) );
286 /* get the next message */
288 if ( (tag = ber_get_next( sb, &len, ber ))
289 != LDAP_TAG_MESSAGE ) {
290 if ( tag == LBER_DEFAULT) {
292 Debug( LDAP_DEBUG_CONNS,
293 "ber_get_next failed.\n", 0, 0, 0 );
296 if (errno==EWOULDBLOCK) return -2;
299 if (errno == EAGAIN) return -2;
301 ld->ld_errno = LDAP_SERVER_DOWN;
304 ld->ld_errno = LDAP_LOCAL_ERROR;
309 * We read a complete message.
310 * The connection should no longer need this ber.
312 lc->lconn_ber = NULLBER;
315 if ( ber_get_int( ber, &id ) == LBER_ERROR ) {
317 ld->ld_errno = LDAP_DECODING_ERROR;
321 /* if it's been abandoned, toss it */
322 if ( ldap_abandoned( ld, (int)id ) ) {
324 return( -2 ); /* continue looking */
327 if (( lr = ldap_find_request_by_msgid( ld, id )) == NULL ) {
328 Debug( LDAP_DEBUG_ANY,
329 "no request for response with msgid %ld (tossing)\n",
332 return( -2 ); /* continue looking */
335 /* the message type */
336 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
337 ld->ld_errno = LDAP_DECODING_ERROR;
342 Debug( LDAP_DEBUG_TRACE, "ldap_read: %s msgid %ld, original id %d\n",
343 ( tag == LDAP_RES_SEARCH_ENTRY ) ? "entry" :
344 ( tag == LDAP_RES_SEARCH_REFERENCE ) ? "reference" : "result",
349 hadref = simple_request = 0;
350 rc = -2; /* default is to keep looking (no response found) */
351 lr->lr_res_msgtype = tag;
353 if ( tag != LDAP_RES_SEARCH_ENTRY ) {
354 if ( ld->ld_version >= LDAP_VERSION2 &&
355 ( lr->lr_parent != NULL ||
356 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ) )
358 tmpber = *ber; /* struct copy */
359 if ( ber_scanf( &tmpber, "{iaa}", &lderr,
360 &lr->lr_res_matched, &lr->lr_res_error )
362 if ( lderr != LDAP_SUCCESS ) {
363 /* referrals are in error string */
364 refer_cnt = ldap_chase_referrals( ld, lr,
365 &lr->lr_res_error, &hadref );
368 /* save errno, message, and matched string */
369 if ( !hadref || lr->lr_res_error == NULL ) {
370 lr->lr_res_errno = ( lderr ==
371 LDAP_PARTIAL_RESULTS ) ? LDAP_SUCCESS
373 } else if ( ld->ld_errno != LDAP_SUCCESS ) {
374 lr->lr_res_errno = ld->ld_errno;
376 lr->lr_res_errno = LDAP_PARTIAL_RESULTS;
378 Debug( LDAP_DEBUG_TRACE,
379 "new result: res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
380 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
381 lr->lr_res_matched ? lr->lr_res_matched : "" );
385 Debug( LDAP_DEBUG_TRACE,
386 "read1msg: %d new referrals\n", refer_cnt, 0, 0 );
388 if ( refer_cnt != 0 ) { /* chasing referrals */
391 if ( refer_cnt < 0 ) {
392 return( -1 ); /* fatal error */
394 lr->lr_status = LDAP_REQST_CHASINGREFS;
396 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
397 /* request without any referrals */
398 simple_request = ( hadref ? 0 : 1 );
400 /* request with referrals or child request */
405 while ( lr->lr_parent != NULL ) {
406 merge_error_info( ld, lr->lr_parent, lr );
409 if ( --lr->lr_outrefcnt > 0 ) {
410 break; /* not completely done yet */
414 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
416 tag = lr->lr_res_msgtype;
417 Debug( LDAP_DEBUG_ANY, "request %ld done\n",
419 Debug( LDAP_DEBUG_TRACE,
420 "res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
421 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
422 lr->lr_res_matched ? lr->lr_res_matched : "" );
423 if ( !simple_request ) {
426 if ( build_result_ber( ld, ber, lr )
428 ld->ld_errno = LDAP_NO_MEMORY;
429 rc = -1; /* fatal error */
433 ldap_free_request( ld, lr );
437 ldap_free_connection( ld, lc, 0, 1 );
446 /* make a new ldap message */
447 if ( (new = (LDAPMessage *) LDAP_CALLOC( 1, sizeof(LDAPMessage) ))
449 ld->ld_errno = LDAP_NO_MEMORY;
452 new->lm_msgid = (int)id;
453 new->lm_msgtype = tag;
457 if ( ld->ld_cache != NULL ) {
458 ldap_add_result_to_cache( ld, new );
460 #endif /* LDAP_NOCACHE */
462 /* is this the one we're looking for? */
463 if ( msgid == LDAP_RES_ANY || id == msgid ) {
465 || (new->lm_msgtype != LDAP_RES_SEARCH_RESULT
466 && new->lm_msgtype != LDAP_RES_SEARCH_ENTRY) ) {
468 ld->ld_errno = LDAP_SUCCESS;
470 } else if ( new->lm_msgtype == LDAP_RES_SEARCH_RESULT) {
471 foundit = 1; /* return the chain later */
476 * if not, we must add it to the list of responses. if
477 * the msgid is already there, it must be part of an existing
482 for ( l = ld->ld_responses; l != NULLMSG; l = l->lm_next ) {
483 if ( l->lm_msgid == new->lm_msgid )
488 /* not part of an existing search response */
489 if ( l == NULLMSG ) {
492 ld->ld_errno = LDAP_SUCCESS;
496 new->lm_next = ld->ld_responses;
497 ld->ld_responses = new;
498 return( -2 ); /* continue looking */
501 Debug( LDAP_DEBUG_TRACE, "adding response id %d type %d:\n",
502 new->lm_msgid, new->lm_msgtype, 0 );
504 /* part of a search response - add to end of list of entries */
505 for ( tmp = l; tmp->lm_chain != NULLMSG &&
506 tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY;
507 tmp = tmp->lm_chain )
511 /* return the whole chain if that's what we were looking for */
513 if ( prev == NULLMSG )
514 ld->ld_responses = l->lm_next;
516 prev->lm_next = l->lm_next;
518 ld->ld_errno = LDAP_SUCCESS;
519 #ifdef LDAP_WORLD_P16
521 * XXX questionable fix; see text for [P16] on
522 * http://www.critical-angle.com/ldapworld/patch/
524 * inclusion of this patch causes searchs to hang on
527 return( l->lm_msgtype );
528 #else /* LDAP_WORLD_P16 */
530 #endif /* !LDAP_WORLD_P16 */
533 return( -2 ); /* continue looking */
538 build_result_ber( LDAP *ld, BerElement *ber, LDAPRequest *lr )
543 ber_init_w_nullc( ber, 0 );
544 ldap_set_ber_options( ld, ber );
545 if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
546 (unsigned long) lr->lr_res_msgtype, lr->lr_res_errno,
547 lr->lr_res_matched ? lr->lr_res_matched : "",
548 lr->lr_res_error ? lr->lr_res_error : "" ) == -1 ) {
549 return( LBER_ERROR );
553 if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) {
554 return( LBER_ERROR );
557 if ( ber_get_int( ber, &along ) == LBER_ERROR ) {
558 return( LBER_ERROR );
561 return( ber_peek_tag( ber, &len ));
566 merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
569 * Merge error information in "lr" with "parentr" error code and string.
571 if ( lr->lr_res_errno == LDAP_PARTIAL_RESULTS ) {
572 parentr->lr_res_errno = lr->lr_res_errno;
573 if ( lr->lr_res_error != NULL ) {
574 (void)ldap_append_referral( ld, &parentr->lr_res_error,
577 } else if ( lr->lr_res_errno != LDAP_SUCCESS &&
578 parentr->lr_res_errno == LDAP_SUCCESS ) {
579 parentr->lr_res_errno = lr->lr_res_errno;
580 if ( parentr->lr_res_error != NULL ) {
581 LDAP_FREE( parentr->lr_res_error );
583 parentr->lr_res_error = lr->lr_res_error;
584 lr->lr_res_error = NULL;
585 if ( LDAP_NAME_ERROR( lr->lr_res_errno )) {
586 if ( parentr->lr_res_matched != NULL ) {
587 LDAP_FREE( parentr->lr_res_matched );
589 parentr->lr_res_matched = lr->lr_res_matched;
590 lr->lr_res_matched = NULL;
594 Debug( LDAP_DEBUG_TRACE, "merged parent (id %d) error info: ",
595 parentr->lr_msgid, 0, 0 );
596 Debug( LDAP_DEBUG_TRACE, "result errno %d, error <%s>, matched <%s>\n",
597 parentr->lr_res_errno, parentr->lr_res_error ?
598 parentr->lr_res_error : "", parentr->lr_res_matched ?
599 parentr->lr_res_matched : "" );
605 ldap_msgtype( LDAPMessage *lm )
607 assert( lm != NULL );
608 return ( lm == NULL ) ? lm->lm_msgtype : -1;
613 ldap_msgid( LDAPMessage *lm )
615 assert( lm != NULL );
617 return ( lm == NULL ) ? lm->lm_msgid : -1;
622 ldap_msgfree( LDAPMessage *lm )
627 assert( lm != NULL );
629 Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
631 for ( ; lm != NULLMSG; lm = next ) {
633 type = lm->lm_msgtype;
634 ber_free( lm->lm_ber, 1 );
635 LDAP_FREE( (char *) lm );
642 * ldap_msgdelete - delete a message. It returns:
643 * 0 if the entire message was deleted
644 * -1 if the message was not found, or only part of it was found
647 ldap_msgdelete( LDAP *ld, int msgid )
649 LDAPMessage *lm, *prev;
651 assert( ld != NULL );
653 Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete\n", 0, 0, 0 );
656 for ( lm = ld->ld_responses; lm != NULLMSG; lm = lm->lm_next ) {
657 if ( lm->lm_msgid == msgid )
665 if ( prev == NULLMSG )
666 ld->ld_responses = lm->lm_next;
668 prev->lm_next = lm->lm_next;
670 if ( ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY )
678 * return 1 if message msgid is waiting to be abandoned, 0 otherwise
681 ldap_abandoned( LDAP *ld, int msgid )
685 if ( ld->ld_abandoned == NULL )
688 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
689 if ( ld->ld_abandoned[i] == msgid )
697 ldap_mark_abandoned( LDAP *ld, int msgid )
701 if ( ld->ld_abandoned == NULL )
704 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
705 if ( ld->ld_abandoned[i] == msgid )
708 if ( ld->ld_abandoned[i] == -1 )
711 for ( ; ld->ld_abandoned[i] != -1; i++ ) {
712 ld->ld_abandoned[i] = ld->ld_abandoned[i + 1];
719 #ifdef LDAP_CONNECTIONLESS
721 cldap_getmsg( LDAP *ld, struct timeval *timeout, BerElement *ber )
724 unsigned long tag, len;
726 if ( ! ber_pvt_sb_data_ready(&ld->ld_sb) ) {
727 rc = ldap_select1( ld, timeout );
728 if ( rc == -1 || rc == 0 ) {
729 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
735 /* get the next message */
736 if ( (tag = ber_get_next( &ld->ld_sb, &len, ber ))
737 != LDAP_TAG_MESSAGE ) {
738 ld->ld_errno = (tag == LBER_DEFAULT ? LDAP_SERVER_DOWN :
745 #endif /* LDAP_CONNECTIONLESS */