2 * Copyright (c) 1990 Regents of the University of Michigan.
5 * result.c - wait for an ldap result
11 static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
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 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 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 * First, look through the list of responses we have received on
66 * this association and see if the response we're interested in
67 * is there. If it is, return it. If not, call wait4msg() to
68 * wait until it arrives or timeout occurs.
71 Debug( LDAP_DEBUG_TRACE, "ldap_result\n", 0, 0, 0 );
75 for ( lm = ld->ld_responses; lm != NULLMSG; lm = nextlm ) {
78 if ( ldap_abandoned( ld, lm->lm_msgid ) ) {
79 ldap_mark_abandoned( ld, lm->lm_msgid );
81 if ( lastlm == NULLMSG ) {
82 ld->ld_responses = lm->lm_next;
84 lastlm->lm_next = nextlm;
92 if ( msgid == LDAP_RES_ANY || lm->lm_msgid == msgid ) {
96 || (lm->lm_msgtype != LDAP_RES_SEARCH_RESULT
97 && lm->lm_msgtype != LDAP_RES_SEARCH_ENTRY) )
100 for ( tmp = lm; tmp != NULLMSG; tmp = tmp->lm_chain ) {
101 if ( tmp->lm_msgtype == LDAP_RES_SEARCH_RESULT )
105 if ( tmp == NULLMSG ) {
106 return( wait4msg( ld, msgid, all, timeout,
114 if ( lm == NULLMSG ) {
115 return( wait4msg( ld, msgid, all, timeout, result ) );
118 if ( lastlm == NULLMSG ) {
119 ld->ld_responses = (all == 0 && lm->lm_chain != NULLMSG
120 ? lm->lm_chain : lm->lm_next);
122 lastlm->lm_next = (all == 0 && lm->lm_chain != NULLMSG
123 ? lm->lm_chain : lm->lm_next);
126 lm->lm_chain = NULLMSG;
127 lm->lm_next = NULLMSG;
130 ld->ld_errno = LDAP_SUCCESS;
131 return( lm->lm_msgtype );
135 wait4msg( LDAP *ld, int msgid, int all, struct timeval *timeout,
136 LDAPMessage **result )
139 struct timeval tv, *tvp;
140 time_t start_time = 0;
142 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
143 LDAPConn *lc, *nextlc;
144 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
147 if ( timeout == NULL ) {
148 Debug( LDAP_DEBUG_TRACE, "wait4msg (infinite timeout)\n",
151 Debug( LDAP_DEBUG_TRACE, "wait4msg (timeout %ld sec, %ld usec)\n",
152 timeout->tv_sec, timeout->tv_usec, 0 );
154 #endif /* LDAP_DEBUG */
156 if ( timeout == NULL ) {
161 start_time = time( NULL );
166 #ifndef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
168 if ( ld->ld_sb.sb_ber.ber_ptr >= ld->ld_sb.sb_ber.ber_end ) {
169 rc = ldap_select1( ld, tvp );
171 if ( rc == 0 || ( rc == -1 && (
172 ( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART)
174 || errno != EINTR ))) {
175 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
182 rc = -2; /* select interrupted: loop */
184 rc = read1msg( ld, msgid, all, &ld->ld_sb, result );
186 #else /* !LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
188 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
189 ldap_dump_connection( ld, ld->ld_conns, 1 );
190 ldap_dump_requests_and_responses( ld );
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,
203 rc = do_ldap_select( ld, tvp );
208 Debug( LDAP_DEBUG_TRACE,
209 "do_ldap_select returned -1: errno %d\n",
214 if ( rc == 0 || ( rc == -1 && (
215 ( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART)
217 || errno != EINTR )))
219 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
225 rc = -2; /* select interrupted: loop */
228 for ( lc = ld->ld_conns; rc == -2 && lc != NULL;
230 nextlc = lc->lconn_next;
231 if ( lc->lconn_status ==
232 LDAP_CONNST_CONNECTED &&
233 ldap_is_read_ready( ld,
235 rc = read1msg( ld, msgid, all,
236 lc->lconn_sb, lc, result );
241 #endif /* !LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
243 if ( rc == -2 && tvp != NULL ) {
244 tmp_time = time( NULL );
245 if (( tv.tv_sec -= ( tmp_time - start_time )) <= 0 ) {
246 rc = 0; /* timed out */
247 ld->ld_errno = LDAP_TIMEOUT;
251 Debug( LDAP_DEBUG_TRACE, "wait4msg: %ld secs to go\n",
253 start_time = tmp_time;
262 read1msg( LDAP *ld, int msgid, int all, Sockbuf *sb,
263 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
265 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
266 LDAPMessage **result )
269 LDAPMessage *new, *l, *prev, *tmp;
271 unsigned long tag, len;
273 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
276 int rc, refer_cnt, hadref, simple_request;
278 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
280 Debug( LDAP_DEBUG_TRACE, "read1msg\n", 0, 0, 0 );
282 ber_init_w_nullc( &ber, 0 );
283 ldap_set_ber_options( ld, &ber );
285 /* get the next message */
286 if ( (tag = ber_get_next( sb, &len, &ber ))
287 != LDAP_TAG_MESSAGE ) {
288 ld->ld_errno = (tag == LBER_DEFAULT ? LDAP_SERVER_DOWN :
294 if ( ber_get_int( &ber, &id ) == LBER_ERROR ) {
295 ld->ld_errno = LDAP_DECODING_ERROR;
299 /* if it's been abandoned, toss it */
300 if ( ldap_abandoned( ld, (int)id ) ) {
301 free( ber.ber_buf ); /* gack! */
302 return( -2 ); /* continue looking */
305 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
306 if (( lr = ldap_find_request_by_msgid( ld, id )) == NULL ) {
307 Debug( LDAP_DEBUG_ANY,
308 "no request for response with msgid %ld (tossing)\n",
310 free( ber.ber_buf ); /* gack! */
311 return( -2 ); /* continue looking */
313 Debug( LDAP_DEBUG_TRACE, "got %s msgid %ld, original id %d\n",
314 ( tag == LDAP_RES_SEARCH_ENTRY ) ? "entry" : "result", id,
317 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
319 /* the message type */
320 if ( (tag = ber_peek_tag( &ber, &len )) == LBER_ERROR ) {
321 ld->ld_errno = LDAP_DECODING_ERROR;
325 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
327 hadref = simple_request = 0;
328 rc = -2; /* default is to keep looking (no response found) */
329 lr->lr_res_msgtype = tag;
331 if ( tag != LDAP_RES_SEARCH_ENTRY ) {
332 if ( ld->ld_version >= LDAP_VERSION2 &&
333 ( lr->lr_parent != NULL ||
334 ( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS)
335 != LDAP_OPT_OFF ) ) )
337 tmpber = ber; /* struct copy */
338 if ( ber_scanf( &tmpber, "{iaa}", &lderr,
339 &lr->lr_res_matched, &lr->lr_res_error )
341 if ( lderr != LDAP_SUCCESS ) {
342 /* referrals are in error string */
343 refer_cnt = ldap_chase_referrals( ld, lr,
344 &lr->lr_res_error, &hadref );
347 /* save errno, message, and matched string */
348 if ( !hadref || lr->lr_res_error == NULL ) {
349 lr->lr_res_errno = ( lderr ==
350 LDAP_PARTIAL_RESULTS ) ? LDAP_SUCCESS
352 } else if ( ld->ld_errno != LDAP_SUCCESS ) {
353 lr->lr_res_errno = ld->ld_errno;
355 lr->lr_res_errno = LDAP_PARTIAL_RESULTS;
357 Debug( LDAP_DEBUG_TRACE,
358 "new result: res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
359 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
360 lr->lr_res_matched ? lr->lr_res_matched : "" );
364 Debug( LDAP_DEBUG_TRACE,
365 "read1msg: %d new referrals\n", refer_cnt, 0, 0 );
367 if ( refer_cnt != 0 ) { /* chasing referrals */
368 free( ber.ber_buf ); /* gack! */
370 if ( refer_cnt < 0 ) {
371 return( -1 ); /* fatal error */
373 lr->lr_status = LDAP_REQST_CHASINGREFS;
375 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
376 /* request without any referrals */
377 simple_request = ( hadref ? 0 : 1 );
379 /* request with referrals or child request */
380 free( ber.ber_buf ); /* gack! */
384 while ( lr->lr_parent != NULL ) {
385 merge_error_info( ld, lr->lr_parent, lr );
388 if ( --lr->lr_outrefcnt > 0 ) {
389 break; /* not completely done yet */
393 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
395 tag = lr->lr_res_msgtype;
396 Debug( LDAP_DEBUG_ANY, "request %ld done\n",
398 Debug( LDAP_DEBUG_TRACE,
399 "res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
400 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
401 lr->lr_res_matched ? lr->lr_res_matched : "" );
402 if ( !simple_request ) {
403 if ( ber.ber_buf != NULL ) {
404 free( ber.ber_buf ); /* gack! */
407 if ( build_result_ber( ld, &ber, lr )
409 ld->ld_errno = LDAP_NO_MEMORY;
410 rc = -1; /* fatal error */
414 ldap_free_request( ld, lr );
418 ldap_free_connection( ld, lc, 0, 1 );
423 if ( ber.ber_buf == NULL ) {
427 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
428 /* make a new ldap message */
429 if ( (new = (LDAPMessage *) calloc( 1, sizeof(LDAPMessage) ))
431 ld->ld_errno = LDAP_NO_MEMORY;
434 new->lm_msgid = (int)id;
435 new->lm_msgtype = tag;
436 new->lm_ber = ber_dup( &ber );
439 if ( ld->ld_cache != NULL ) {
440 ldap_add_result_to_cache( ld, new );
442 #endif /* LDAP_NOCACHE */
444 /* is this the one we're looking for? */
445 if ( msgid == LDAP_RES_ANY || id == msgid ) {
447 || (new->lm_msgtype != LDAP_RES_SEARCH_RESULT
448 && new->lm_msgtype != LDAP_RES_SEARCH_ENTRY) ) {
450 ld->ld_errno = LDAP_SUCCESS;
452 } else if ( new->lm_msgtype == LDAP_RES_SEARCH_RESULT) {
453 foundit = 1; /* return the chain later */
458 * if not, we must add it to the list of responses. if
459 * the msgid is already there, it must be part of an existing
464 for ( l = ld->ld_responses; l != NULLMSG; l = l->lm_next ) {
465 if ( l->lm_msgid == new->lm_msgid )
470 /* not part of an existing search response */
471 if ( l == NULLMSG ) {
474 ld->ld_errno = LDAP_SUCCESS;
478 new->lm_next = ld->ld_responses;
479 ld->ld_responses = new;
480 return( -2 ); /* continue looking */
483 Debug( LDAP_DEBUG_TRACE, "adding response id %d type %d:\n",
484 new->lm_msgid, new->lm_msgtype, 0 );
486 /* part of a search response - add to end of list of entries */
487 for ( tmp = l; tmp->lm_chain != NULLMSG &&
488 tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY;
489 tmp = tmp->lm_chain )
493 /* return the whole chain if that's what we were looking for */
495 if ( prev == NULLMSG )
496 ld->ld_responses = l->lm_next;
498 prev->lm_next = l->lm_next;
500 ld->ld_errno = LDAP_SUCCESS;
501 #ifdef LDAP_WORLD_P16
503 * XXX questionable fix; see text for [P16] on
504 * http://www.critical-angle.com/ldapworld/patch/
506 * inclusion of this patch causes searchs to hang on
509 return( l->lm_msgtype );
510 #else /* LDAP_WORLD_P16 */
512 #endif /* !LDAP_WORLD_P16 */
515 return( -2 ); /* continue looking */
519 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
521 build_result_ber( LDAP *ld, BerElement *ber, LDAPRequest *lr )
526 ber_init_w_nullc( ber, 0 );
527 ldap_set_ber_options( ld, ber );
528 if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
529 (long)lr->lr_res_msgtype, lr->lr_res_errno,
530 lr->lr_res_matched ? lr->lr_res_matched : "",
531 lr->lr_res_error ? lr->lr_res_error : "" ) == -1 ) {
532 return( LBER_ERROR );
536 if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) {
537 return( LBER_ERROR );
540 if ( ber_get_int( ber, &along ) == LBER_ERROR ) {
541 return( LBER_ERROR );
544 return( ber_peek_tag( ber, &len ));
549 merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
552 * Merge error information in "lr" with "parentr" error code and string.
554 if ( lr->lr_res_errno == LDAP_PARTIAL_RESULTS ) {
555 parentr->lr_res_errno = lr->lr_res_errno;
556 if ( lr->lr_res_error != NULL ) {
557 (void)ldap_append_referral( ld, &parentr->lr_res_error,
560 } else if ( lr->lr_res_errno != LDAP_SUCCESS &&
561 parentr->lr_res_errno == LDAP_SUCCESS ) {
562 parentr->lr_res_errno = lr->lr_res_errno;
563 if ( parentr->lr_res_error != NULL ) {
564 free( parentr->lr_res_error );
566 parentr->lr_res_error = lr->lr_res_error;
567 lr->lr_res_error = NULL;
568 if ( NAME_ERROR( lr->lr_res_errno )) {
569 if ( parentr->lr_res_matched != NULL ) {
570 free( parentr->lr_res_matched );
572 parentr->lr_res_matched = lr->lr_res_matched;
573 lr->lr_res_matched = NULL;
577 Debug( LDAP_DEBUG_TRACE, "merged parent (id %d) error info: ",
578 parentr->lr_msgid, 0, 0 );
579 Debug( LDAP_DEBUG_TRACE, "result errno %d, error <%s>, matched <%s>\n",
580 parentr->lr_res_errno, parentr->lr_res_error ?
581 parentr->lr_res_error : "", parentr->lr_res_matched ?
582 parentr->lr_res_matched : "" );
584 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
588 #if defined( LDAP_CONNECTIONLESS ) || !defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS )
591 ldap_select1( LDAP *ld, struct timeval *timeout )
596 if ( tblsize == 0 ) {
598 tblsize = sysconf( _SC_OPEN_MAX );
599 #elif HAVE_GETDTABLESIZE
600 tblsize = getdtablesize();
602 tblsize = FD_SETSIZE;
605 if ( tblsize > FD_SETSIZE ) {
606 tblsize = FD_SETSIZE;
608 #endif /* FD_SETSIZE */
612 FD_SET( ld->ld_sb.sb_sd, &readfds );
614 return( select( tblsize, &readfds, 0, 0, timeout ) );
617 #endif /* !LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
621 ldap_msgtype( LDAPMessage *lm )
623 return( lm ? lm->lm_msgtype : -1 );
627 ldap_msgid( LDAPMessage *lm )
629 return( lm ? lm->lm_msgid : -1 );
634 ldap_msgfree( LDAPMessage *lm )
639 Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
641 for ( ; lm != NULLMSG; lm = next ) {
643 type = lm->lm_msgtype;
644 ber_free( lm->lm_ber, 1 );
652 * ldap_msgdelete - delete a message. It returns:
653 * 0 if the entire message was deleted
654 * -1 if the message was not found, or only part of it was found
657 ldap_msgdelete( LDAP *ld, int msgid )
659 LDAPMessage *lm, *prev;
661 Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete\n", 0, 0, 0 );
664 for ( lm = ld->ld_responses; lm != NULLMSG; lm = lm->lm_next ) {
665 if ( lm->lm_msgid == msgid )
673 if ( prev == NULLMSG )
674 ld->ld_responses = lm->lm_next;
676 prev->lm_next = lm->lm_next;
678 if ( ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY )
686 * return 1 if message msgid is waiting to be abandoned, 0 otherwise
689 ldap_abandoned( LDAP *ld, int msgid )
693 if ( ld->ld_abandoned == NULL )
696 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
697 if ( ld->ld_abandoned[i] == msgid )
705 ldap_mark_abandoned( LDAP *ld, int msgid )
709 if ( ld->ld_abandoned == NULL )
712 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
713 if ( ld->ld_abandoned[i] == msgid )
716 if ( ld->ld_abandoned[i] == -1 )
719 for ( ; ld->ld_abandoned[i] != -1; i++ ) {
720 ld->ld_abandoned[i] = ld->ld_abandoned[i + 1];
727 #ifdef LDAP_CONNECTIONLESS
729 cldap_getmsg( LDAP *ld, struct timeval *timeout, BerElement *ber )
732 unsigned long tag, len;
734 if ( ld->ld_sb.sb_ber.ber_ptr >= ld->ld_sb.sb_ber.ber_end ) {
735 rc = ldap_select1( ld, timeout );
736 if ( rc == -1 || rc == 0 ) {
737 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
743 /* get the next message */
744 if ( (tag = ber_get_next( &ld->ld_sb, &len, ber ))
745 != LDAP_TAG_MESSAGE ) {
746 ld->ld_errno = (tag == LBER_DEFAULT ? LDAP_SERVER_DOWN :
753 #endif /* LDAP_CONNECTIONLESS */