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 ));
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_REFERRALS */
36 static int read1msg LDAP_P(( LDAP *ld, int msgid, int all, Sockbuf *sb,
37 LDAPMessage **result ));
38 #endif /* LDAP_REFERRALS */
39 #if defined( LDAP_CONNECTIONLESS ) || !defined( LDAP_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_REFERRALS
143 LDAPConn *lc, *nextlc;
144 #endif /* LDAP_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_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, LDAP_BOOL_RESTART) = LDAP_OPT_OFF)
173 || errno != EINTR ))) {
174 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
181 rc = -2; /* select interrupted: loop */
183 rc = read1msg( ld, msgid, all, &ld->ld_sb, result );
185 #else /* !LDAP_REFERRALS */
187 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
188 ldap_dump_connection( ld, ld->ld_conns, 1 );
189 ldap_dump_requests_and_responses( ld );
191 #endif /* LDAP_DEBUG */
192 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
193 if ( lc->lconn_sb->sb_ber.ber_ptr <
194 lc->lconn_sb->sb_ber.ber_end ) {
195 rc = read1msg( ld, msgid, all, lc->lconn_sb,
202 rc = do_ldap_select( ld, tvp );
207 Debug( LDAP_DEBUG_TRACE,
208 "do_ldap_select returned -1: errno %d\n",
213 if ( rc == 0 || ( rc == -1 && (
214 (LDAP_BOOL_GET(ld, LDAP_BOOL_RESTART) == LDAP_OPT_OFF)
215 || errno != EINTR ))) {
216 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
222 rc = -2; /* select interrupted: loop */
225 for ( lc = ld->ld_conns; rc == -2 && lc != NULL;
227 nextlc = lc->lconn_next;
228 if ( lc->lconn_status ==
229 LDAP_CONNST_CONNECTED &&
230 ldap_is_read_ready( ld,
232 rc = read1msg( ld, msgid, all,
233 lc->lconn_sb, lc, result );
238 #endif /* !LDAP_REFERRALS */
240 if ( rc == -2 && tvp != NULL ) {
241 tmp_time = time( NULL );
242 if (( tv.tv_sec -= ( tmp_time - start_time )) <= 0 ) {
243 rc = 0; /* timed out */
244 ld->ld_errno = LDAP_TIMEOUT;
248 Debug( LDAP_DEBUG_TRACE, "wait4msg: %ld secs to go\n",
250 start_time = tmp_time;
259 read1msg( LDAP *ld, int msgid, int all, Sockbuf *sb,
260 #ifdef LDAP_REFERRALS
262 #endif /* LDAP_REFERRALS */
263 LDAPMessage **result )
266 LDAPMessage *new, *l, *prev, *tmp;
268 unsigned long tag, len;
270 #ifdef LDAP_REFERRALS
273 int rc, refer_cnt, hadref, simple_request;
275 #endif /* LDAP_REFERRALS */
277 Debug( LDAP_DEBUG_TRACE, "read1msg\n", 0, 0, 0 );
279 ber_init_w_nullc( &ber, 0 );
280 ldap_set_ber_options( ld, &ber );
282 /* get the next message */
283 if ( (tag = ber_get_next( sb, &len, &ber ))
284 != LDAP_TAG_MESSAGE ) {
285 ld->ld_errno = (tag == LBER_DEFAULT ? LDAP_SERVER_DOWN :
291 if ( ber_get_int( &ber, &id ) == LBER_ERROR ) {
292 ld->ld_errno = LDAP_DECODING_ERROR;
296 /* if it's been abandoned, toss it */
297 if ( ldap_abandoned( ld, (int)id ) ) {
298 free( ber.ber_buf ); /* gack! */
299 return( -2 ); /* continue looking */
302 #ifdef LDAP_REFERRALS
303 if (( lr = ldap_find_request_by_msgid( ld, id )) == NULL ) {
304 Debug( LDAP_DEBUG_ANY,
305 "no request for response with msgid %ld (tossing)\n",
307 free( ber.ber_buf ); /* gack! */
308 return( -2 ); /* continue looking */
310 Debug( LDAP_DEBUG_TRACE, "got %s msgid %ld, original id %d\n",
311 ( tag == LDAP_RES_SEARCH_ENTRY ) ? "entry" : "result", id,
314 #endif /* LDAP_REFERRALS */
316 /* the message type */
317 if ( (tag = ber_peek_tag( &ber, &len )) == LBER_ERROR ) {
318 ld->ld_errno = LDAP_DECODING_ERROR;
322 #ifdef LDAP_REFERRALS
324 hadref = simple_request = 0;
325 rc = -2; /* default is to keep looking (no response found) */
326 lr->lr_res_msgtype = tag;
328 if ( tag != LDAP_RES_SEARCH_ENTRY ) {
329 if ( ld->ld_version >= LDAP_VERSION2 &&
330 ( lr->lr_parent != NULL ||
331 (LDAP_BOOL_GET(ld, LDAP_BOOL_REFERRALS) != LDAP_OPT_OFF))) {
332 tmpber = ber; /* struct copy */
333 if ( ber_scanf( &tmpber, "{iaa}", &lderr,
334 &lr->lr_res_matched, &lr->lr_res_error )
336 if ( lderr != LDAP_SUCCESS ) {
337 /* referrals are in error string */
338 refer_cnt = ldap_chase_referrals( ld, lr,
339 &lr->lr_res_error, &hadref );
342 /* save errno, message, and matched string */
343 if ( !hadref || lr->lr_res_error == NULL ) {
344 lr->lr_res_errno = ( lderr ==
345 LDAP_PARTIAL_RESULTS ) ? LDAP_SUCCESS
347 } else if ( ld->ld_errno != LDAP_SUCCESS ) {
348 lr->lr_res_errno = ld->ld_errno;
350 lr->lr_res_errno = LDAP_PARTIAL_RESULTS;
352 Debug( LDAP_DEBUG_TRACE,
353 "new result: res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
354 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
355 lr->lr_res_matched ? lr->lr_res_matched : "" );
359 Debug( LDAP_DEBUG_TRACE,
360 "read1msg: %d new referrals\n", refer_cnt, 0, 0 );
362 if ( refer_cnt != 0 ) { /* chasing referrals */
363 free( ber.ber_buf ); /* gack! */
365 if ( refer_cnt < 0 ) {
366 return( -1 ); /* fatal error */
368 lr->lr_status = LDAP_REQST_CHASINGREFS;
370 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
371 /* request without any referrals */
372 simple_request = ( hadref ? 0 : 1 );
374 /* request with referrals or child request */
375 free( ber.ber_buf ); /* gack! */
379 while ( lr->lr_parent != NULL ) {
380 merge_error_info( ld, lr->lr_parent, lr );
383 if ( --lr->lr_outrefcnt > 0 ) {
384 break; /* not completely done yet */
388 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
390 tag = lr->lr_res_msgtype;
391 Debug( LDAP_DEBUG_ANY, "request %ld done\n",
393 Debug( LDAP_DEBUG_TRACE,
394 "res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
395 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
396 lr->lr_res_matched ? lr->lr_res_matched : "" );
397 if ( !simple_request ) {
398 if ( ber.ber_buf != NULL ) {
399 free( ber.ber_buf ); /* gack! */
402 if ( build_result_ber( ld, &ber, lr )
404 ld->ld_errno = LDAP_NO_MEMORY;
405 rc = -1; /* fatal error */
409 ldap_free_request( ld, lr );
413 ldap_free_connection( ld, lc, 0, 1 );
418 if ( ber.ber_buf == NULL ) {
422 #endif /* LDAP_REFERRALS */
423 /* make a new ldap message */
424 if ( (new = (LDAPMessage *) calloc( 1, sizeof(LDAPMessage) ))
426 ld->ld_errno = LDAP_NO_MEMORY;
429 new->lm_msgid = (int)id;
430 new->lm_msgtype = tag;
431 new->lm_ber = ber_dup( &ber );
434 if ( ld->ld_cache != NULL ) {
435 ldap_add_result_to_cache( ld, new );
437 #endif /* LDAP_NOCACHE */
439 /* is this the one we're looking for? */
440 if ( msgid == LDAP_RES_ANY || id == msgid ) {
442 || (new->lm_msgtype != LDAP_RES_SEARCH_RESULT
443 && new->lm_msgtype != LDAP_RES_SEARCH_ENTRY) ) {
445 ld->ld_errno = LDAP_SUCCESS;
447 } else if ( new->lm_msgtype == LDAP_RES_SEARCH_RESULT) {
448 foundit = 1; /* return the chain later */
453 * if not, we must add it to the list of responses. if
454 * the msgid is already there, it must be part of an existing
459 for ( l = ld->ld_responses; l != NULLMSG; l = l->lm_next ) {
460 if ( l->lm_msgid == new->lm_msgid )
465 /* not part of an existing search response */
466 if ( l == NULLMSG ) {
469 ld->ld_errno = LDAP_SUCCESS;
473 new->lm_next = ld->ld_responses;
474 ld->ld_responses = new;
475 return( -2 ); /* continue looking */
478 Debug( LDAP_DEBUG_TRACE, "adding response id %d type %d:\n",
479 new->lm_msgid, new->lm_msgtype, 0 );
481 /* part of a search response - add to end of list of entries */
482 for ( tmp = l; tmp->lm_chain != NULLMSG &&
483 tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY;
484 tmp = tmp->lm_chain )
488 /* return the whole chain if that's what we were looking for */
490 if ( prev == NULLMSG )
491 ld->ld_responses = l->lm_next;
493 prev->lm_next = l->lm_next;
495 ld->ld_errno = LDAP_SUCCESS;
496 #ifdef LDAP_WORLD_P16
498 * XXX questionable fix; see text for [P16] on
499 * http://www.critical-angle.com/ldapworld/patch/
501 * inclusion of this patch causes searchs to hang on
504 return( l->lm_msgtype );
505 #else /* LDAP_WORLD_P16 */
507 #endif /* !LDAP_WORLD_P16 */
510 return( -2 ); /* continue looking */
514 #ifdef LDAP_REFERRALS
516 build_result_ber( LDAP *ld, BerElement *ber, LDAPRequest *lr )
521 ber_init_w_nullc( ber, 0 );
522 ldap_set_ber_options( ld, ber );
523 if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
524 (long)lr->lr_res_msgtype, lr->lr_res_errno,
525 lr->lr_res_matched ? lr->lr_res_matched : "",
526 lr->lr_res_error ? lr->lr_res_error : "" ) == -1 ) {
527 return( LBER_ERROR );
531 if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) {
532 return( LBER_ERROR );
535 if ( ber_get_int( ber, &along ) == LBER_ERROR ) {
536 return( LBER_ERROR );
539 return( ber_peek_tag( ber, &len ));
544 merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
547 * Merge error information in "lr" with "parentr" error code and string.
549 if ( lr->lr_res_errno == LDAP_PARTIAL_RESULTS ) {
550 parentr->lr_res_errno = lr->lr_res_errno;
551 if ( lr->lr_res_error != NULL ) {
552 (void)ldap_append_referral( ld, &parentr->lr_res_error,
555 } else if ( lr->lr_res_errno != LDAP_SUCCESS &&
556 parentr->lr_res_errno == LDAP_SUCCESS ) {
557 parentr->lr_res_errno = lr->lr_res_errno;
558 if ( parentr->lr_res_error != NULL ) {
559 free( parentr->lr_res_error );
561 parentr->lr_res_error = lr->lr_res_error;
562 lr->lr_res_error = NULL;
563 if ( NAME_ERROR( lr->lr_res_errno )) {
564 if ( parentr->lr_res_matched != NULL ) {
565 free( parentr->lr_res_matched );
567 parentr->lr_res_matched = lr->lr_res_matched;
568 lr->lr_res_matched = NULL;
572 Debug( LDAP_DEBUG_TRACE, "merged parent (id %d) error info: ",
573 parentr->lr_msgid, 0, 0 );
574 Debug( LDAP_DEBUG_TRACE, "result errno %d, error <%s>, matched <%s>\n",
575 parentr->lr_res_errno, parentr->lr_res_error ?
576 parentr->lr_res_error : "", parentr->lr_res_matched ?
577 parentr->lr_res_matched : "" );
579 #endif /* LDAP_REFERRALS */
583 #if defined( LDAP_CONNECTIONLESS ) || !defined( LDAP_REFERRALS )
586 ldap_select1( LDAP *ld, struct timeval *timeout )
591 if ( tblsize == 0 ) {
593 tblsize = sysconf( _SC_OPEN_MAX );
594 #elif HAVE_GETDTABLESIZE
595 tblsize = getdtablesize();
597 tblsize = FD_SETSIZE;
600 if ( tblsize > FD_SETSIZE ) {
601 tblsize = FD_SETSIZE;
603 #endif /* FD_SETSIZE */
607 FD_SET( ld->ld_sb.sb_sd, &readfds );
609 return( select( tblsize, &readfds, 0, 0, timeout ) );
612 #endif /* !LDAP_REFERRALS */
616 ldap_msgfree( LDAPMessage *lm )
621 Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
623 for ( ; lm != NULLMSG; lm = next ) {
625 type = lm->lm_msgtype;
626 ber_free( lm->lm_ber, 1 );
634 * ldap_msgdelete - delete a message. It returns:
635 * 0 if the entire message was deleted
636 * -1 if the message was not found, or only part of it was found
639 ldap_msgdelete( LDAP *ld, int msgid )
641 LDAPMessage *lm, *prev;
643 Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete\n", 0, 0, 0 );
646 for ( lm = ld->ld_responses; lm != NULLMSG; lm = lm->lm_next ) {
647 if ( lm->lm_msgid == msgid )
655 if ( prev == NULLMSG )
656 ld->ld_responses = lm->lm_next;
658 prev->lm_next = lm->lm_next;
660 if ( ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY )
668 * return 1 if message msgid is waiting to be abandoned, 0 otherwise
671 ldap_abandoned( LDAP *ld, int msgid )
675 if ( ld->ld_abandoned == NULL )
678 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
679 if ( ld->ld_abandoned[i] == msgid )
687 ldap_mark_abandoned( LDAP *ld, int msgid )
691 if ( ld->ld_abandoned == NULL )
694 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
695 if ( ld->ld_abandoned[i] == msgid )
698 if ( ld->ld_abandoned[i] == -1 )
701 for ( ; ld->ld_abandoned[i] != -1; i++ ) {
702 ld->ld_abandoned[i] = ld->ld_abandoned[i + 1];
709 #ifdef LDAP_CONNECTIONLESS
711 cldap_getmsg( LDAP *ld, struct timeval *timeout, BerElement *ber )
714 unsigned long tag, len;
716 if ( ld->ld_sb.sb_ber.ber_ptr >= ld->ld_sb.sb_ber.ber_end ) {
717 rc = ldap_select1( ld, timeout );
718 if ( rc == -1 || rc == 0 ) {
719 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
725 /* get the next message */
726 if ( (tag = ber_get_next( &ld->ld_sb, &len, ber ))
727 != LDAP_TAG_MESSAGE ) {
728 ld->ld_errno = (tag == LBER_DEFAULT ? LDAP_SERVER_DOWN :
735 #endif /* LDAP_CONNECTIONLESS */