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 **bp, 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 rc = -1; /* fatal error */
432 ldap_free_request( ld, lr );
436 ldap_free_connection( ld, lc, 0, 1 );
445 /* make a new ldap message */
446 if ( (new = (LDAPMessage *) LDAP_CALLOC( 1, sizeof(LDAPMessage) ))
448 ld->ld_errno = LDAP_NO_MEMORY;
451 new->lm_msgid = (int)id;
452 new->lm_msgtype = tag;
456 if ( ld->ld_cache != NULL ) {
457 ldap_add_result_to_cache( ld, new );
459 #endif /* LDAP_NOCACHE */
461 /* is this the one we're looking for? */
462 if ( msgid == LDAP_RES_ANY || id == msgid ) {
464 || (new->lm_msgtype != LDAP_RES_SEARCH_RESULT
465 && new->lm_msgtype != LDAP_RES_SEARCH_ENTRY) ) {
467 ld->ld_errno = LDAP_SUCCESS;
469 } else if ( new->lm_msgtype == LDAP_RES_SEARCH_RESULT) {
470 foundit = 1; /* return the chain later */
475 * if not, we must add it to the list of responses. if
476 * the msgid is already there, it must be part of an existing
481 for ( l = ld->ld_responses; l != NULLMSG; l = l->lm_next ) {
482 if ( l->lm_msgid == new->lm_msgid )
487 /* not part of an existing search response */
488 if ( l == NULLMSG ) {
491 ld->ld_errno = LDAP_SUCCESS;
495 new->lm_next = ld->ld_responses;
496 ld->ld_responses = new;
497 return( -2 ); /* continue looking */
500 Debug( LDAP_DEBUG_TRACE, "adding response id %d type %d:\n",
501 new->lm_msgid, new->lm_msgtype, 0 );
503 /* part of a search response - add to end of list of entries */
504 for ( tmp = l; tmp->lm_chain != NULLMSG &&
505 tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY;
506 tmp = tmp->lm_chain )
510 /* return the whole chain if that's what we were looking for */
512 if ( prev == NULLMSG )
513 ld->ld_responses = l->lm_next;
515 prev->lm_next = l->lm_next;
517 ld->ld_errno = LDAP_SUCCESS;
518 #ifdef LDAP_WORLD_P16
520 * XXX questionable fix; see text for [P16] on
521 * http://www.critical-angle.com/ldapworld/patch/
523 * inclusion of this patch causes searchs to hang on
526 return( l->lm_msgtype );
527 #else /* LDAP_WORLD_P16 */
529 #endif /* !LDAP_WORLD_P16 */
532 return( -2 ); /* continue looking */
537 build_result_ber( LDAP *ld, BerElement **bp, LDAPRequest *lr )
539 unsigned long len, tag;
544 ber = ldap_alloc_ber_with_options( ld );
547 ld->ld_errno = LDAP_NO_MEMORY;
551 if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
552 (unsigned long) lr->lr_res_msgtype, lr->lr_res_errno,
553 lr->lr_res_matched ? lr->lr_res_matched : "",
554 lr->lr_res_error ? lr->lr_res_error : "" ) == -1 ) {
556 ld->ld_errno = LDAP_ENCODING_ERROR;
558 return( LBER_ERROR );
563 if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) {
564 ld->ld_errno = LDAP_DECODING_ERROR;
566 return( LBER_ERROR );
569 if ( ber_get_int( ber, &along ) == LBER_ERROR ) {
570 ld->ld_errno = LDAP_DECODING_ERROR;
572 return( LBER_ERROR );
575 tag = ber_peek_tag( ber, &len );
577 if ( tag == LBER_ERROR ) {
578 ld->ld_errno = LDAP_DECODING_ERROR;
580 return( LBER_ERROR );
589 merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
592 * Merge error information in "lr" with "parentr" error code and string.
594 if ( lr->lr_res_errno == LDAP_PARTIAL_RESULTS ) {
595 parentr->lr_res_errno = lr->lr_res_errno;
596 if ( lr->lr_res_error != NULL ) {
597 (void)ldap_append_referral( ld, &parentr->lr_res_error,
600 } else if ( lr->lr_res_errno != LDAP_SUCCESS &&
601 parentr->lr_res_errno == LDAP_SUCCESS ) {
602 parentr->lr_res_errno = lr->lr_res_errno;
603 if ( parentr->lr_res_error != NULL ) {
604 LDAP_FREE( parentr->lr_res_error );
606 parentr->lr_res_error = lr->lr_res_error;
607 lr->lr_res_error = NULL;
608 if ( LDAP_NAME_ERROR( lr->lr_res_errno )) {
609 if ( parentr->lr_res_matched != NULL ) {
610 LDAP_FREE( parentr->lr_res_matched );
612 parentr->lr_res_matched = lr->lr_res_matched;
613 lr->lr_res_matched = NULL;
617 Debug( LDAP_DEBUG_TRACE, "merged parent (id %d) error info: ",
618 parentr->lr_msgid, 0, 0 );
619 Debug( LDAP_DEBUG_TRACE, "result errno %d, error <%s>, matched <%s>\n",
620 parentr->lr_res_errno, parentr->lr_res_error ?
621 parentr->lr_res_error : "", parentr->lr_res_matched ?
622 parentr->lr_res_matched : "" );
628 ldap_msgtype( LDAPMessage *lm )
630 assert( lm != NULL );
631 return ( lm == NULL ) ? lm->lm_msgtype : -1;
636 ldap_msgid( LDAPMessage *lm )
638 assert( lm != NULL );
640 return ( lm == NULL ) ? lm->lm_msgid : -1;
645 ldap_msgfree( LDAPMessage *lm )
650 assert( lm != NULL );
652 Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
654 for ( ; lm != NULLMSG; lm = next ) {
656 type = lm->lm_msgtype;
657 ber_free( lm->lm_ber, 1 );
658 LDAP_FREE( (char *) lm );
665 * ldap_msgdelete - delete a message. It returns:
666 * 0 if the entire message was deleted
667 * -1 if the message was not found, or only part of it was found
670 ldap_msgdelete( LDAP *ld, int msgid )
672 LDAPMessage *lm, *prev;
674 assert( ld != NULL );
676 Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete\n", 0, 0, 0 );
679 for ( lm = ld->ld_responses; lm != NULLMSG; lm = lm->lm_next ) {
680 if ( lm->lm_msgid == msgid )
688 if ( prev == NULLMSG )
689 ld->ld_responses = lm->lm_next;
691 prev->lm_next = lm->lm_next;
693 if ( ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY )
701 * return 1 if message msgid is waiting to be abandoned, 0 otherwise
704 ldap_abandoned( LDAP *ld, int msgid )
708 if ( ld->ld_abandoned == NULL )
711 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
712 if ( ld->ld_abandoned[i] == msgid )
720 ldap_mark_abandoned( LDAP *ld, int msgid )
724 if ( ld->ld_abandoned == NULL )
727 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
728 if ( ld->ld_abandoned[i] == msgid )
731 if ( ld->ld_abandoned[i] == -1 )
734 for ( ; ld->ld_abandoned[i] != -1; i++ ) {
735 ld->ld_abandoned[i] = ld->ld_abandoned[i + 1];
742 #ifdef LDAP_CONNECTIONLESS
744 cldap_getmsg( LDAP *ld, struct timeval *timeout, BerElement *ber )
747 unsigned long tag, len;
749 if ( ! ber_pvt_sb_data_ready(&ld->ld_sb) ) {
750 rc = ldap_select1( ld, timeout );
751 if ( rc == -1 || rc == 0 ) {
752 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
758 /* get the next message */
759 if ( (tag = ber_get_next( &ld->ld_sb, &len, ber ))
760 != LDAP_TAG_MESSAGE ) {
761 ld->ld_errno = (tag == LBER_DEFAULT ? LDAP_SERVER_DOWN :
768 #endif /* LDAP_CONNECTIONLESS */