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 int 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 && (( ld->ld_options &
172 LDAP_OPT_RESTART ) == 0 || errno != EINTR ))) {
173 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
180 rc = -2; /* select interrupted: loop */
182 rc = read1msg( ld, msgid, all, &ld->ld_sb, result );
184 #else /* !LDAP_REFERRALS */
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 ( lc->lconn_sb->sb_ber.ber_ptr <
193 lc->lconn_sb->sb_ber.ber_end ) {
194 rc = read1msg( ld, msgid, all, lc->lconn_sb,
201 rc = do_ldap_select( ld, tvp );
206 Debug( LDAP_DEBUG_TRACE,
207 "do_ldap_select returned -1: errno %d\n",
212 if ( rc == 0 || ( rc == -1 && (( ld->ld_options &
213 LDAP_OPT_RESTART ) == 0 || errno != EINTR ))) {
214 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
220 rc = -2; /* select interrupted: loop */
223 for ( lc = ld->ld_conns; rc == -2 && lc != NULL;
225 nextlc = lc->lconn_next;
226 if ( lc->lconn_status ==
227 LDAP_CONNST_CONNECTED &&
228 ldap_is_read_ready( ld,
230 rc = read1msg( ld, msgid, all,
231 lc->lconn_sb, lc, result );
236 #endif /* !LDAP_REFERRALS */
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",
248 start_time = tmp_time;
257 read1msg( LDAP *ld, int msgid, int all, Sockbuf *sb,
258 #ifdef LDAP_REFERRALS
260 #endif /* LDAP_REFERRALS */
261 LDAPMessage **result )
264 LDAPMessage *new, *l, *prev, *tmp;
266 unsigned long tag, len;
268 #ifdef LDAP_REFERRALS
271 int rc, refer_cnt, hadref, simple_request;
273 #endif /* LDAP_REFERRALS */
275 Debug( LDAP_DEBUG_TRACE, "read1msg\n", 0, 0, 0 );
278 ldap_set_ber_options( ld, &ber );
280 /* get the next message */
281 if ( (tag = ber_get_next( sb, &len, &ber ))
282 != LDAP_TAG_MESSAGE ) {
283 ld->ld_errno = (tag == LBER_DEFAULT ? LDAP_SERVER_DOWN :
289 if ( ber_get_int( &ber, &id ) == LBER_ERROR ) {
290 ld->ld_errno = LDAP_DECODING_ERROR;
294 /* if it's been abandoned, toss it */
295 if ( ldap_abandoned( ld, (int)id ) ) {
296 free( ber.ber_buf ); /* gack! */
297 return( -2 ); /* continue looking */
300 #ifdef LDAP_REFERRALS
301 if (( lr = ldap_find_request_by_msgid( ld, id )) == NULL ) {
302 Debug( LDAP_DEBUG_ANY,
303 "no request for response with msgid %ld (tossing)\n",
305 free( ber.ber_buf ); /* gack! */
306 return( -2 ); /* continue looking */
308 Debug( LDAP_DEBUG_TRACE, "got %s msgid %ld, original id %d\n",
309 ( tag == LDAP_RES_SEARCH_ENTRY ) ? "entry" : "result", id,
312 #endif /* LDAP_REFERRALS */
314 /* the message type */
315 if ( (tag = ber_peek_tag( &ber, &len )) == LBER_ERROR ) {
316 ld->ld_errno = LDAP_DECODING_ERROR;
320 #ifdef LDAP_REFERRALS
322 hadref = simple_request = 0;
323 rc = -2; /* default is to keep looking (no response found) */
324 lr->lr_res_msgtype = tag;
326 if ( tag != LDAP_RES_SEARCH_ENTRY ) {
327 if ( ld->ld_version >= LDAP_VERSION2 &&
328 ( lr->lr_parent != NULL ||
329 ( ld->ld_options & LDAP_OPT_REFERRALS ) != 0 )) {
330 tmpber = ber; /* struct copy */
331 if ( ber_scanf( &tmpber, "{iaa}", &lderr,
332 &lr->lr_res_matched, &lr->lr_res_error )
334 if ( lderr != LDAP_SUCCESS ) {
335 /* referrals are in error string */
336 refer_cnt = ldap_chase_referrals( ld, lr,
337 &lr->lr_res_error, &hadref );
340 /* save errno, message, and matched string */
341 if ( !hadref || lr->lr_res_error == NULL ) {
342 lr->lr_res_errno = ( lderr ==
343 LDAP_PARTIAL_RESULTS ) ? LDAP_SUCCESS
345 } else if ( ld->ld_errno != LDAP_SUCCESS ) {
346 lr->lr_res_errno = ld->ld_errno;
348 lr->lr_res_errno = LDAP_PARTIAL_RESULTS;
350 Debug( LDAP_DEBUG_TRACE,
351 "new result: res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
352 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
353 lr->lr_res_matched ? lr->lr_res_matched : "" );
357 Debug( LDAP_DEBUG_TRACE,
358 "read1msg: %d new referrals\n", refer_cnt, 0, 0 );
360 if ( refer_cnt != 0 ) { /* chasing referrals */
361 free( ber.ber_buf ); /* gack! */
363 if ( refer_cnt < 0 ) {
364 return( -1 ); /* fatal error */
366 lr->lr_status = LDAP_REQST_CHASINGREFS;
368 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
369 /* request without any referrals */
370 simple_request = ( hadref ? 0 : 1 );
372 /* request with referrals or child request */
373 free( ber.ber_buf ); /* gack! */
377 while ( lr->lr_parent != NULL ) {
378 merge_error_info( ld, lr->lr_parent, lr );
381 if ( --lr->lr_outrefcnt > 0 ) {
382 break; /* not completely done yet */
386 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
388 tag = lr->lr_res_msgtype;
389 Debug( LDAP_DEBUG_ANY, "request %ld done\n",
391 Debug( LDAP_DEBUG_TRACE,
392 "res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
393 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
394 lr->lr_res_matched ? lr->lr_res_matched : "" );
395 if ( !simple_request ) {
396 if ( ber.ber_buf != NULL ) {
397 free( ber.ber_buf ); /* gack! */
400 if ( build_result_ber( ld, &ber, lr )
402 ld->ld_errno = LDAP_NO_MEMORY;
403 rc = -1; /* fatal error */
407 ldap_free_request( ld, lr );
411 ldap_free_connection( ld, lc, 0, 1 );
416 if ( ber.ber_buf == NULL ) {
420 #endif /* LDAP_REFERRALS */
421 /* make a new ldap message */
422 if ( (new = (LDAPMessage *) calloc( 1, sizeof(LDAPMessage) ))
424 ld->ld_errno = LDAP_NO_MEMORY;
427 new->lm_msgid = (int)id;
428 new->lm_msgtype = tag;
429 new->lm_ber = ber_dup( &ber );
432 if ( ld->ld_cache != NULL ) {
433 ldap_add_result_to_cache( ld, new );
435 #endif /* LDAP_NOCACHE */
437 /* is this the one we're looking for? */
438 if ( msgid == LDAP_RES_ANY || id == msgid ) {
440 || (new->lm_msgtype != LDAP_RES_SEARCH_RESULT
441 && new->lm_msgtype != LDAP_RES_SEARCH_ENTRY) ) {
443 ld->ld_errno = LDAP_SUCCESS;
445 } else if ( new->lm_msgtype == LDAP_RES_SEARCH_RESULT) {
446 foundit = 1; /* return the chain later */
451 * if not, we must add it to the list of responses. if
452 * the msgid is already there, it must be part of an existing
457 for ( l = ld->ld_responses; l != NULLMSG; l = l->lm_next ) {
458 if ( l->lm_msgid == new->lm_msgid )
463 /* not part of an existing search response */
464 if ( l == NULLMSG ) {
467 ld->ld_errno = LDAP_SUCCESS;
471 new->lm_next = ld->ld_responses;
472 ld->ld_responses = new;
473 return( -2 ); /* continue looking */
476 Debug( LDAP_DEBUG_TRACE, "adding response id %d type %d:\n",
477 new->lm_msgid, new->lm_msgtype, 0 );
479 /* part of a search response - add to end of list of entries */
480 for ( tmp = l; tmp->lm_chain != NULLMSG &&
481 tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY;
482 tmp = tmp->lm_chain )
486 /* return the whole chain if that's what we were looking for */
488 if ( prev == NULLMSG )
489 ld->ld_responses = l->lm_next;
491 prev->lm_next = l->lm_next;
493 ld->ld_errno = LDAP_SUCCESS;
494 #ifdef LDAP_WORLD_P16
496 * XXX questionable fix; see text for [P16] on
497 * http://www.critical-angle.com/ldapworld/patch/
499 * inclusion of this patch causes searchs to hang on
502 return( l->lm_msgtype );
503 #else /* LDAP_WORLD_P16 */
505 #endif /* !LDAP_WORLD_P16 */
508 return( -2 ); /* continue looking */
512 #ifdef LDAP_REFERRALS
514 build_result_ber( LDAP *ld, BerElement *ber, LDAPRequest *lr )
520 ldap_set_ber_options( ld, ber );
521 if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
522 (long)lr->lr_res_msgtype, lr->lr_res_errno,
523 lr->lr_res_matched ? lr->lr_res_matched : "",
524 lr->lr_res_error ? lr->lr_res_error : "" ) == LBER_ERROR ) {
525 return( LBER_ERROR );
529 if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) {
530 return( LBER_ERROR );
533 if ( ber_get_int( ber, &along ) == LBER_ERROR ) {
534 return( LBER_ERROR );
537 return( ber_peek_tag( ber, &len ));
542 merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
545 * Merge error information in "lr" with "parentr" error code and string.
547 if ( lr->lr_res_errno == LDAP_PARTIAL_RESULTS ) {
548 parentr->lr_res_errno = lr->lr_res_errno;
549 if ( lr->lr_res_error != NULL ) {
550 (void)ldap_append_referral( ld, &parentr->lr_res_error,
553 } else if ( lr->lr_res_errno != LDAP_SUCCESS &&
554 parentr->lr_res_errno == LDAP_SUCCESS ) {
555 parentr->lr_res_errno = lr->lr_res_errno;
556 if ( parentr->lr_res_error != NULL ) {
557 free( parentr->lr_res_error );
559 parentr->lr_res_error = lr->lr_res_error;
560 lr->lr_res_error = NULL;
561 if ( NAME_ERROR( lr->lr_res_errno )) {
562 if ( parentr->lr_res_matched != NULL ) {
563 free( parentr->lr_res_matched );
565 parentr->lr_res_matched = lr->lr_res_matched;
566 lr->lr_res_matched = NULL;
570 Debug( LDAP_DEBUG_TRACE, "merged parent (id %d) error info: ",
571 parentr->lr_msgid, 0, 0 );
572 Debug( LDAP_DEBUG_TRACE, "result errno %d, error <%s>, matched <%s>\n",
573 parentr->lr_res_errno, parentr->lr_res_error ?
574 parentr->lr_res_error : "", parentr->lr_res_matched ?
575 parentr->lr_res_matched : "" );
577 #endif /* LDAP_REFERRALS */
581 #if defined( LDAP_CONNECTIONLESS ) || !defined( LDAP_REFERRALS )
584 ldap_select1( LDAP *ld, struct timeval *timeout )
589 if ( tblsize == 0 ) {
591 tblsize = sysconf( _SC_OPEN_MAX );
592 #elif HAVE_GETDTABLESIZE
593 tblsize = getdtablesize();
595 tblsize = FD_SETSIZE;
598 if ( tblsize > FD_SETSIZE ) {
599 tblsize = FD_SETSIZE;
601 #endif /* FD_SETSIZE */
605 FD_SET( ld->ld_sb.sb_sd, &readfds );
607 return( select( tblsize, &readfds, 0, 0, timeout ) );
610 #endif /* !LDAP_REFERRALS */
614 ldap_msgfree( LDAPMessage *lm )
619 Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
621 for ( ; lm != NULLMSG; lm = next ) {
623 type = lm->lm_msgtype;
624 ber_free( lm->lm_ber, 1 );
632 * ldap_msgdelete - delete a message. It returns:
633 * 0 if the entire message was deleted
634 * -1 if the message was not found, or only part of it was found
637 ldap_msgdelete( LDAP *ld, int msgid )
639 LDAPMessage *lm, *prev;
641 Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete\n", 0, 0, 0 );
644 for ( lm = ld->ld_responses; lm != NULLMSG; lm = lm->lm_next ) {
645 if ( lm->lm_msgid == msgid )
653 if ( prev == NULLMSG )
654 ld->ld_responses = lm->lm_next;
656 prev->lm_next = lm->lm_next;
658 if ( ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY )
666 * return 1 if message msgid is waiting to be abandoned, 0 otherwise
669 ldap_abandoned( LDAP *ld, int msgid )
673 if ( ld->ld_abandoned == NULL )
676 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
677 if ( ld->ld_abandoned[i] == msgid )
685 ldap_mark_abandoned( LDAP *ld, int msgid )
689 if ( ld->ld_abandoned == NULL )
692 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
693 if ( ld->ld_abandoned[i] == msgid )
696 if ( ld->ld_abandoned[i] == -1 )
699 for ( ; ld->ld_abandoned[i] != -1; i++ ) {
700 ld->ld_abandoned[i] = ld->ld_abandoned[i + 1];
707 #ifdef LDAP_CONNECTIONLESS
709 cldap_getmsg( LDAP *ld, struct timeval *timeout, BerElement *ber )
712 unsigned long tag, len;
714 if ( ld->ld_sb.sb_ber.ber_ptr >= ld->ld_sb.sb_ber.ber_end ) {
715 rc = ldap_select1( ld, timeout );
716 if ( rc == -1 || rc == 0 ) {
717 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
723 /* get the next message */
724 if ( (tag = ber_get_next( &ld->ld_sb, &len, ber ))
725 != LDAP_TAG_MESSAGE ) {
726 ld->ld_errno = (tag == LBER_DEFAULT ? LDAP_SERVER_DOWN :
733 #endif /* LDAP_CONNECTIONLESS */