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 static int try_read1msg LDAP_P(( LDAP *ld, int msgid, int all, Sockbuf *sb, LDAPConn *lc,
31 LDAPMessage **result ));
32 static unsigned long build_result_ber LDAP_P(( LDAP *ld, BerElement *ber, LDAPRequest *lr ));
33 static void merge_error_info LDAP_P(( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr ));
37 * ldap_result - wait for an ldap result response to a message from the
38 * ldap server. If msgid is -1, any message will be accepted, otherwise
39 * ldap_result will wait for a response with msgid. If all is 0 the
40 * first message with id msgid will be accepted, otherwise, ldap_result
41 * will wait for all responses with id msgid and then return a pointer to
42 * the entire list of messages. This is only useful for search responses,
43 * which can be of two message types (zero or more entries, followed by an
44 * ldap result). The type of the first message received is returned.
45 * When waiting, any messages that have been abandoned are discarded.
48 * ldap_result( s, msgid, all, timeout, result )
51 ldap_result( LDAP *ld, int msgid, int all, struct timeval *timeout,
52 LDAPMessage **result )
54 LDAPMessage *lm, *lastlm, *nextlm;
57 assert( result != NULL );
59 Debug( LDAP_DEBUG_TRACE, "ldap_result\n", 0, 0, 0 );
65 if( result == NULL ) {
66 ld->ld_errno = LDAP_PARAM_ERROR;
71 * First, look through the list of responses we have received on
72 * this association and see if the response we're interested in
73 * is there. If it is, return it. If not, call wait4msg() to
74 * wait until it arrives or timeout occurs.
79 for ( lm = ld->ld_responses; lm != NULLMSG; lm = nextlm ) {
82 if ( ldap_abandoned( ld, lm->lm_msgid ) ) {
83 ldap_mark_abandoned( ld, lm->lm_msgid );
85 if ( lastlm == NULLMSG ) {
86 ld->ld_responses = lm->lm_next;
88 lastlm->lm_next = nextlm;
96 if ( msgid == LDAP_RES_ANY || lm->lm_msgid == msgid ) {
100 || (lm->lm_msgtype != LDAP_RES_SEARCH_RESULT
101 && lm->lm_msgtype != LDAP_RES_SEARCH_REFERENCE /* LDAPv3 */
102 && lm->lm_msgtype != LDAP_RES_SEARCH_ENTRY) )
105 for ( tmp = lm; tmp != NULLMSG; tmp = tmp->lm_chain ) {
106 if ( tmp->lm_msgtype == LDAP_RES_SEARCH_RESULT )
110 if ( tmp == NULLMSG ) {
111 return( wait4msg( ld, msgid, all, timeout,
119 if ( lm == NULLMSG ) {
120 return( wait4msg( ld, msgid, all, timeout, result ) );
123 if ( lastlm == NULLMSG ) {
124 ld->ld_responses = (all == 0 && lm->lm_chain != NULLMSG
125 ? lm->lm_chain : lm->lm_next);
127 lastlm->lm_next = (all == 0 && lm->lm_chain != NULLMSG
128 ? lm->lm_chain : lm->lm_next);
131 lm->lm_chain = NULLMSG;
132 lm->lm_next = NULLMSG;
135 ld->ld_errno = LDAP_SUCCESS;
136 return( lm->lm_msgtype );
140 wait4msg( LDAP *ld, int msgid, int all, struct timeval *timeout,
141 LDAPMessage **result )
144 struct timeval tv, *tvp;
145 time_t start_time = 0;
147 LDAPConn *lc, *nextlc;
149 assert( ld != NULL );
150 assert( result != NULL );
153 if ( timeout == NULL ) {
154 Debug( LDAP_DEBUG_TRACE, "wait4msg (infinite timeout)\n",
157 Debug( LDAP_DEBUG_TRACE, "wait4msg (timeout %ld sec, %ld usec)\n",
158 (long) timeout->tv_sec, (long) timeout->tv_usec, 0 );
160 #endif /* LDAP_DEBUG */
162 if ( timeout == NULL ) {
167 start_time = time( NULL );
173 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
174 ldap_dump_connection( ld, ld->ld_conns, 1 );
175 ldap_dump_requests_and_responses( ld );
177 #endif /* LDAP_DEBUG */
178 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
179 if ( ber_pvt_sb_data_ready(lc->lconn_sb) ) {
180 rc = try_read1msg( ld, msgid, all, lc->lconn_sb,
187 rc = do_ldap_select( ld, tvp );
192 Debug( LDAP_DEBUG_TRACE,
193 "do_ldap_select returned -1: errno %d\n",
198 if ( rc == 0 || ( rc == -1 && (
199 !LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART)
200 || errno != EINTR )))
202 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
208 rc = -2; /* select interrupted: loop */
211 for ( lc = ld->ld_conns; rc == -2 && lc != NULL;
213 nextlc = lc->lconn_next;
214 if ( lc->lconn_status ==
215 LDAP_CONNST_CONNECTED &&
216 ldap_is_read_ready( ld,
218 rc = try_read1msg( ld, msgid, all,
219 lc->lconn_sb, lc, result );
225 if ( rc == -2 && tvp != NULL ) {
226 tmp_time = time( NULL );
227 if (( tv.tv_sec -= ( tmp_time - start_time )) <= 0 ) {
228 rc = 0; /* timed out */
229 ld->ld_errno = LDAP_TIMEOUT;
233 Debug( LDAP_DEBUG_TRACE, "wait4msg: %ld secs to go\n",
234 (long) tv.tv_sec, 0, 0 );
235 start_time = tmp_time;
244 try_read1msg( LDAP *ld, int msgid, int all, Sockbuf *sb,
245 LDAPConn *lc, LDAPMessage **result )
248 LDAPMessage *new, *l, *prev, *tmp;
250 unsigned long tag, len;
254 int rc, refer_cnt, hadref, simple_request;
257 assert( ld != NULL );
258 assert( lc != NULL );
260 Debug( LDAP_DEBUG_TRACE, "read1msg\n", 0, 0, 0 );
262 if ( lc->lconn_ber == NULLBER ) {
263 lc->lconn_ber = ldap_alloc_ber_with_options(ld);
265 if( lc->lconn_ber == NULL ) {
271 assert( BER_VALID (ber) );
273 /* get the next message */
275 if ( (tag = ber_get_next( sb, &len, ber ))
276 != LDAP_TAG_MESSAGE ) {
277 if ( tag == LBER_DEFAULT) {
279 Debug( LDAP_DEBUG_CONNS,
280 "ber_get_next failed.\n", 0, 0, 0 );
283 if (errno==EWOULDBLOCK) return -2;
286 if (errno == EAGAIN) return -2;
288 ld->ld_errno = LDAP_SERVER_DOWN;
291 ld->ld_errno = LDAP_LOCAL_ERROR;
296 * We read a complete message.
297 * The connection should no longer need this ber.
299 lc->lconn_ber = NULLBER;
302 if ( ber_get_int( ber, &id ) == LBER_ERROR ) {
304 ld->ld_errno = LDAP_DECODING_ERROR;
308 /* if it's been abandoned, toss it */
309 if ( ldap_abandoned( ld, (int)id ) ) {
311 return( -2 ); /* continue looking */
314 if (( lr = ldap_find_request_by_msgid( ld, id )) == NULL ) {
315 Debug( LDAP_DEBUG_ANY,
316 "no request for response with msgid %ld (tossing)\n",
319 return( -2 ); /* continue looking */
322 /* the message type */
323 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
324 ld->ld_errno = LDAP_DECODING_ERROR;
329 Debug( LDAP_DEBUG_TRACE, "ldap_read: %s msgid %ld, original id %d\n",
330 ( tag == LDAP_RES_SEARCH_ENTRY ) ? "entry" :
331 ( tag == LDAP_RES_SEARCH_REFERENCE ) ? "reference" : "result",
336 hadref = simple_request = 0;
337 rc = -2; /* default is to keep looking (no response found) */
338 lr->lr_res_msgtype = tag;
340 if ( tag != LDAP_RES_SEARCH_ENTRY ) {
341 if ( ld->ld_version >= LDAP_VERSION2 &&
342 ( lr->lr_parent != NULL ||
343 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ) )
345 tmpber = *ber; /* struct copy */
346 if ( ber_scanf( &tmpber, "{iaa}", &lderr,
347 &lr->lr_res_matched, &lr->lr_res_error )
349 if ( lderr != LDAP_SUCCESS ) {
350 /* referrals are in error string */
351 refer_cnt = ldap_chase_referrals( ld, lr,
352 &lr->lr_res_error, &hadref );
355 /* save errno, message, and matched string */
356 if ( !hadref || lr->lr_res_error == NULL ) {
357 lr->lr_res_errno = ( lderr ==
358 LDAP_PARTIAL_RESULTS ) ? LDAP_SUCCESS
360 } else if ( ld->ld_errno != LDAP_SUCCESS ) {
361 lr->lr_res_errno = ld->ld_errno;
363 lr->lr_res_errno = LDAP_PARTIAL_RESULTS;
365 Debug( LDAP_DEBUG_TRACE,
366 "new result: res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
367 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
368 lr->lr_res_matched ? lr->lr_res_matched : "" );
372 Debug( LDAP_DEBUG_TRACE,
373 "read1msg: %d new referrals\n", refer_cnt, 0, 0 );
375 if ( refer_cnt != 0 ) { /* chasing referrals */
378 if ( refer_cnt < 0 ) {
379 return( -1 ); /* fatal error */
381 lr->lr_status = LDAP_REQST_CHASINGREFS;
383 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
384 /* request without any referrals */
385 simple_request = ( hadref ? 0 : 1 );
387 /* request with referrals or child request */
392 while ( lr->lr_parent != NULL ) {
393 merge_error_info( ld, lr->lr_parent, lr );
396 if ( --lr->lr_outrefcnt > 0 ) {
397 break; /* not completely done yet */
401 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
403 tag = lr->lr_res_msgtype;
404 Debug( LDAP_DEBUG_ANY, "request %ld done\n",
406 Debug( LDAP_DEBUG_TRACE,
407 "res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
408 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
409 lr->lr_res_matched ? lr->lr_res_matched : "" );
410 if ( !simple_request ) {
413 if ( build_result_ber( ld, ber, lr )
415 ld->ld_errno = LDAP_NO_MEMORY;
416 rc = -1; /* fatal error */
420 ldap_free_request( ld, lr );
424 ldap_free_connection( ld, lc, 0, 1 );
433 /* make a new ldap message */
434 if ( (new = (LDAPMessage *) LDAP_CALLOC( 1, sizeof(LDAPMessage) ))
436 ld->ld_errno = LDAP_NO_MEMORY;
439 new->lm_msgid = (int)id;
440 new->lm_msgtype = tag;
444 if ( ld->ld_cache != NULL ) {
445 ldap_add_result_to_cache( ld, new );
447 #endif /* LDAP_NOCACHE */
449 /* is this the one we're looking for? */
450 if ( msgid == LDAP_RES_ANY || id == msgid ) {
452 || (new->lm_msgtype != LDAP_RES_SEARCH_RESULT
453 && new->lm_msgtype != LDAP_RES_SEARCH_ENTRY) ) {
455 ld->ld_errno = LDAP_SUCCESS;
457 } else if ( new->lm_msgtype == LDAP_RES_SEARCH_RESULT) {
458 foundit = 1; /* return the chain later */
463 * if not, we must add it to the list of responses. if
464 * the msgid is already there, it must be part of an existing
469 for ( l = ld->ld_responses; l != NULLMSG; l = l->lm_next ) {
470 if ( l->lm_msgid == new->lm_msgid )
475 /* not part of an existing search response */
476 if ( l == NULLMSG ) {
479 ld->ld_errno = LDAP_SUCCESS;
483 new->lm_next = ld->ld_responses;
484 ld->ld_responses = new;
485 return( -2 ); /* continue looking */
488 Debug( LDAP_DEBUG_TRACE, "adding response id %d type %d:\n",
489 new->lm_msgid, new->lm_msgtype, 0 );
491 /* part of a search response - add to end of list of entries */
492 for ( tmp = l; tmp->lm_chain != NULLMSG &&
493 tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY;
494 tmp = tmp->lm_chain )
498 /* return the whole chain if that's what we were looking for */
500 if ( prev == NULLMSG )
501 ld->ld_responses = l->lm_next;
503 prev->lm_next = l->lm_next;
505 ld->ld_errno = LDAP_SUCCESS;
506 #ifdef LDAP_WORLD_P16
508 * XXX questionable fix; see text for [P16] on
509 * http://www.critical-angle.com/ldapworld/patch/
511 * inclusion of this patch causes searchs to hang on
514 return( l->lm_msgtype );
515 #else /* LDAP_WORLD_P16 */
517 #endif /* !LDAP_WORLD_P16 */
520 return( -2 ); /* continue looking */
525 build_result_ber( LDAP *ld, BerElement *ber, LDAPRequest *lr )
530 ber_init_w_nullc( ber, 0 );
531 ldap_set_ber_options( ld, ber );
532 if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
533 (unsigned long) lr->lr_res_msgtype, lr->lr_res_errno,
534 lr->lr_res_matched ? lr->lr_res_matched : "",
535 lr->lr_res_error ? lr->lr_res_error : "" ) == -1 ) {
536 return( LBER_ERROR );
540 if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) {
541 return( LBER_ERROR );
544 if ( ber_get_int( ber, &along ) == LBER_ERROR ) {
545 return( LBER_ERROR );
548 return( ber_peek_tag( ber, &len ));
553 merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
556 * Merge error information in "lr" with "parentr" error code and string.
558 if ( lr->lr_res_errno == LDAP_PARTIAL_RESULTS ) {
559 parentr->lr_res_errno = lr->lr_res_errno;
560 if ( lr->lr_res_error != NULL ) {
561 (void)ldap_append_referral( ld, &parentr->lr_res_error,
564 } else if ( lr->lr_res_errno != LDAP_SUCCESS &&
565 parentr->lr_res_errno == LDAP_SUCCESS ) {
566 parentr->lr_res_errno = lr->lr_res_errno;
567 if ( parentr->lr_res_error != NULL ) {
568 LDAP_FREE( parentr->lr_res_error );
570 parentr->lr_res_error = lr->lr_res_error;
571 lr->lr_res_error = NULL;
572 if ( LDAP_NAME_ERROR( lr->lr_res_errno )) {
573 if ( parentr->lr_res_matched != NULL ) {
574 LDAP_FREE( parentr->lr_res_matched );
576 parentr->lr_res_matched = lr->lr_res_matched;
577 lr->lr_res_matched = NULL;
581 Debug( LDAP_DEBUG_TRACE, "merged parent (id %d) error info: ",
582 parentr->lr_msgid, 0, 0 );
583 Debug( LDAP_DEBUG_TRACE, "result errno %d, error <%s>, matched <%s>\n",
584 parentr->lr_res_errno, parentr->lr_res_error ?
585 parentr->lr_res_error : "", parentr->lr_res_matched ?
586 parentr->lr_res_matched : "" );
592 ldap_msgtype( LDAPMessage *lm )
594 assert( lm != NULL );
595 return ( lm == NULL ) ? lm->lm_msgtype : -1;
600 ldap_msgid( LDAPMessage *lm )
602 assert( lm != NULL );
604 return ( lm == NULL ) ? lm->lm_msgid : -1;
609 ldap_msgfree( LDAPMessage *lm )
614 assert( lm != NULL );
616 Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
618 for ( ; lm != NULLMSG; lm = next ) {
620 type = lm->lm_msgtype;
621 ber_free( lm->lm_ber, 1 );
622 LDAP_FREE( (char *) lm );
629 * ldap_msgdelete - delete a message. It returns:
630 * 0 if the entire message was deleted
631 * -1 if the message was not found, or only part of it was found
634 ldap_msgdelete( LDAP *ld, int msgid )
636 LDAPMessage *lm, *prev;
638 assert( ld != NULL );
640 Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete\n", 0, 0, 0 );
643 for ( lm = ld->ld_responses; lm != NULLMSG; lm = lm->lm_next ) {
644 if ( lm->lm_msgid == msgid )
652 if ( prev == NULLMSG )
653 ld->ld_responses = lm->lm_next;
655 prev->lm_next = lm->lm_next;
657 if ( ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY )
665 * return 1 if message msgid is waiting to be abandoned, 0 otherwise
668 ldap_abandoned( LDAP *ld, int msgid )
672 if ( ld->ld_abandoned == NULL )
675 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
676 if ( ld->ld_abandoned[i] == msgid )
684 ldap_mark_abandoned( LDAP *ld, int msgid )
688 if ( ld->ld_abandoned == NULL )
691 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
692 if ( ld->ld_abandoned[i] == msgid )
695 if ( ld->ld_abandoned[i] == -1 )
698 for ( ; ld->ld_abandoned[i] != -1; i++ ) {
699 ld->ld_abandoned[i] = ld->ld_abandoned[i + 1];
706 #ifdef LDAP_CONNECTIONLESS
708 cldap_getmsg( LDAP *ld, struct timeval *timeout, BerElement *ber )
711 unsigned long tag, len;
713 if ( ! ber_pvt_sb_data_ready(&ld->ld_sb) ) {
714 rc = ldap_select1( ld, timeout );
715 if ( rc == -1 || rc == 0 ) {
716 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
722 /* get the next message */
723 if ( (tag = ber_get_next( &ld->ld_sb, &len, ber ))
724 != LDAP_TAG_MESSAGE ) {
725 ld->ld_errno = (tag == LBER_DEFAULT ? LDAP_SERVER_DOWN :
732 #endif /* LDAP_CONNECTIONLESS */