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>
28 static int ldap_abandoned LDAP_P(( LDAP *ld, int msgid ));
29 static int ldap_mark_abandoned LDAP_P(( LDAP *ld, int msgid ));
30 static int wait4msg LDAP_P(( LDAP *ld, int msgid, int all, struct timeval *timeout,
31 LDAPMessage **result ));
33 static int read1msg LDAP_P(( LDAP *ld, int msgid, int all, Sockbuf *sb, LDAPConn *lc,
34 LDAPMessage **result ));
35 static int build_result_ber LDAP_P(( LDAP *ld, BerElement *ber, LDAPRequest *lr ));
36 static void merge_error_info LDAP_P(( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr ));
37 #else /* LDAP_REFERRALS */
38 static int read1msg LDAP_P(( LDAP *ld, int msgid, int all, Sockbuf *sb,
39 LDAPMessage **result ));
40 #endif /* LDAP_REFERRALS */
41 #if defined( LDAP_CONNECTIONLESS ) || !defined( LDAP_REFERRALS )
42 static int ldap_select1 LDAP_P(( LDAP *ld, struct timeval *timeout ));
47 * ldap_result - wait for an ldap result response to a message from the
48 * ldap server. If msgid is -1, any message will be accepted, otherwise
49 * ldap_result will wait for a response with msgid. If all is 0 the
50 * first message with id msgid will be accepted, otherwise, ldap_result
51 * will wait for all responses with id msgid and then return a pointer to
52 * the entire list of messages. This is only useful for search responses,
53 * which can be of two message types (zero or more entries, followed by an
54 * ldap result). The type of the first message received is returned.
55 * When waiting, any messages that have been abandoned are discarded.
58 * ldap_result( s, msgid, all, timeout, result )
61 ldap_result( LDAP *ld, int msgid, int all, struct timeval *timeout,
62 LDAPMessage **result )
64 LDAPMessage *lm, *lastlm, *nextlm;
67 * First, look through the list of responses we have received on
68 * this association and see if the response we're interested in
69 * is there. If it is, return it. If not, call wait4msg() to
70 * wait until it arrives or timeout occurs.
73 Debug( LDAP_DEBUG_TRACE, "ldap_result\n", 0, 0, 0 );
77 for ( lm = ld->ld_responses; lm != NULLMSG; lm = nextlm ) {
80 if ( ldap_abandoned( ld, lm->lm_msgid ) ) {
81 ldap_mark_abandoned( ld, lm->lm_msgid );
83 if ( lastlm == NULLMSG ) {
84 ld->ld_responses = lm->lm_next;
86 lastlm->lm_next = nextlm;
94 if ( msgid == LDAP_RES_ANY || lm->lm_msgid == msgid ) {
98 || (lm->lm_msgtype != LDAP_RES_SEARCH_RESULT
99 && lm->lm_msgtype != LDAP_RES_SEARCH_ENTRY) )
102 for ( tmp = lm; tmp != NULLMSG; tmp = tmp->lm_chain ) {
103 if ( tmp->lm_msgtype == LDAP_RES_SEARCH_RESULT )
107 if ( tmp == NULLMSG ) {
108 return( wait4msg( ld, msgid, all, timeout,
116 if ( lm == NULLMSG ) {
117 return( wait4msg( ld, msgid, all, timeout, result ) );
120 if ( lastlm == NULLMSG ) {
121 ld->ld_responses = (all == 0 && lm->lm_chain != NULLMSG
122 ? lm->lm_chain : lm->lm_next);
124 lastlm->lm_next = (all == 0 && lm->lm_chain != NULLMSG
125 ? lm->lm_chain : lm->lm_next);
128 lm->lm_chain = NULLMSG;
129 lm->lm_next = NULLMSG;
132 ld->ld_errno = LDAP_SUCCESS;
133 return( lm->lm_msgtype );
137 wait4msg( LDAP *ld, int msgid, int all, struct timeval *timeout,
138 LDAPMessage **result )
141 struct timeval tv, *tvp;
142 time_t start_time = 0;
144 #ifdef LDAP_REFERRALS
145 LDAPConn *lc, *nextlc;
146 #endif /* LDAP_REFERRALS */
149 if ( timeout == NULL ) {
150 Debug( LDAP_DEBUG_TRACE, "wait4msg (infinite timeout)\n",
153 Debug( LDAP_DEBUG_TRACE, "wait4msg (timeout %ld sec, %ld usec)\n",
154 timeout->tv_sec, timeout->tv_usec, 0 );
156 #endif /* LDAP_DEBUG */
158 if ( timeout == NULL ) {
163 start_time = time( NULL );
168 #ifndef LDAP_REFERRALS
170 if ( ld->ld_sb.sb_ber.ber_ptr >= ld->ld_sb.sb_ber.ber_end ) {
171 rc = ldap_select1( ld, tvp );
173 #if !defined( MACOS ) && !defined( DOS )
174 if ( rc == 0 || ( rc == -1 && (( ld->ld_options &
175 LDAP_OPT_RESTART ) == 0 || errno != EINTR ))) {
177 if ( rc == -1 || rc == 0 ) {
179 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
186 rc = -2; /* select interrupted: loop */
188 rc = read1msg( ld, msgid, all, &ld->ld_sb, result );
190 #else /* !LDAP_REFERRALS */
192 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
193 ldap_dump_connection( ld, ld->ld_conns, 1 );
194 ldap_dump_requests_and_responses( ld );
196 #endif /* LDAP_DEBUG */
197 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
198 if ( lc->lconn_sb->sb_ber.ber_ptr <
199 lc->lconn_sb->sb_ber.ber_end ) {
200 rc = read1msg( ld, msgid, all, lc->lconn_sb,
207 rc = do_ldap_select( ld, tvp );
210 #if defined( LDAP_DEBUG ) && !defined( MACOS ) && !defined( DOS )
212 Debug( LDAP_DEBUG_TRACE,
213 "do_ldap_select returned -1: errno %d\n",
218 #if !defined( MACOS ) && !defined( DOS )
219 if ( rc == 0 || ( rc == -1 && (( ld->ld_options &
220 LDAP_OPT_RESTART ) == 0 || errno != EINTR ))) {
222 if ( rc == -1 || rc == 0 ) {
224 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
230 rc = -2; /* select interrupted: loop */
233 for ( lc = ld->ld_conns; rc == -2 && lc != NULL;
235 nextlc = lc->lconn_next;
236 if ( lc->lconn_status ==
237 LDAP_CONNST_CONNECTED &&
238 ldap_is_read_ready( ld,
240 rc = read1msg( ld, msgid, all,
241 lc->lconn_sb, lc, result );
246 #endif /* !LDAP_REFERRALS */
248 if ( rc == -2 && tvp != NULL ) {
249 tmp_time = time( NULL );
250 if (( tv.tv_sec -= ( tmp_time - start_time )) <= 0 ) {
251 rc = 0; /* timed out */
252 ld->ld_errno = LDAP_TIMEOUT;
256 Debug( LDAP_DEBUG_TRACE, "wait4msg: %ld secs to go\n",
258 start_time = tmp_time;
267 read1msg( LDAP *ld, int msgid, int all, Sockbuf *sb,
268 #ifdef LDAP_REFERRALS
270 #endif /* LDAP_REFERRALS */
271 LDAPMessage **result )
274 LDAPMessage *new, *l, *prev, *tmp;
276 unsigned long tag, len;
278 #ifdef LDAP_REFERRALS
281 int rc, refer_cnt, hadref, simple_request;
283 #endif /* LDAP_REFERRALS */
285 Debug( LDAP_DEBUG_TRACE, "read1msg\n", 0, 0, 0 );
288 ldap_set_ber_options( ld, &ber );
290 /* get the next message */
291 if ( (tag = ber_get_next( sb, &len, &ber ))
292 != LDAP_TAG_MESSAGE ) {
293 ld->ld_errno = (tag == LBER_DEFAULT ? LDAP_SERVER_DOWN :
299 if ( ber_get_int( &ber, &id ) == LBER_ERROR ) {
300 ld->ld_errno = LDAP_DECODING_ERROR;
304 /* if it's been abandoned, toss it */
305 if ( ldap_abandoned( ld, (int)id ) ) {
306 free( ber.ber_buf ); /* gack! */
307 return( -2 ); /* continue looking */
310 #ifdef LDAP_REFERRALS
311 if (( lr = ldap_find_request_by_msgid( ld, id )) == NULL ) {
312 Debug( LDAP_DEBUG_ANY,
313 "no request for response with msgid %ld (tossing)\n",
315 free( ber.ber_buf ); /* gack! */
316 return( -2 ); /* continue looking */
318 Debug( LDAP_DEBUG_TRACE, "got %s msgid %ld, original id %d\n",
319 ( tag == LDAP_RES_SEARCH_ENTRY ) ? "entry" : "result", id,
322 #endif /* LDAP_REFERRALS */
324 /* the message type */
325 if ( (tag = ber_peek_tag( &ber, &len )) == LBER_ERROR ) {
326 ld->ld_errno = LDAP_DECODING_ERROR;
330 #ifdef LDAP_REFERRALS
332 hadref = simple_request = 0;
333 rc = -2; /* default is to keep looking (no response found) */
334 lr->lr_res_msgtype = tag;
336 if ( tag != LDAP_RES_SEARCH_ENTRY ) {
337 if ( ld->ld_version >= LDAP_VERSION2 &&
338 ( lr->lr_parent != NULL ||
339 ( ld->ld_options & LDAP_OPT_REFERRALS ) != 0 )) {
340 tmpber = ber; /* struct copy */
341 if ( ber_scanf( &tmpber, "{iaa}", &lderr,
342 &lr->lr_res_matched, &lr->lr_res_error )
344 if ( lderr != LDAP_SUCCESS ) {
345 /* referrals are in error string */
346 refer_cnt = ldap_chase_referrals( ld, lr,
347 &lr->lr_res_error, &hadref );
350 /* save errno, message, and matched string */
351 if ( !hadref || lr->lr_res_error == NULL ) {
352 lr->lr_res_errno = ( lderr ==
353 LDAP_PARTIAL_RESULTS ) ? LDAP_SUCCESS
355 } else if ( ld->ld_errno != LDAP_SUCCESS ) {
356 lr->lr_res_errno = ld->ld_errno;
358 lr->lr_res_errno = LDAP_PARTIAL_RESULTS;
360 Debug( LDAP_DEBUG_TRACE,
361 "new result: res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
362 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
363 lr->lr_res_matched ? lr->lr_res_matched : "" );
367 Debug( LDAP_DEBUG_TRACE,
368 "read1msg: %d new referrals\n", refer_cnt, 0, 0 );
370 if ( refer_cnt != 0 ) { /* chasing referrals */
371 free( ber.ber_buf ); /* gack! */
373 if ( refer_cnt < 0 ) {
374 return( -1 ); /* fatal error */
376 lr->lr_status = LDAP_REQST_CHASINGREFS;
378 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
379 /* request without any referrals */
380 simple_request = ( hadref ? 0 : 1 );
382 /* request with referrals or child request */
383 free( ber.ber_buf ); /* gack! */
387 while ( lr->lr_parent != NULL ) {
388 merge_error_info( ld, lr->lr_parent, lr );
391 if ( --lr->lr_outrefcnt > 0 ) {
392 break; /* not completely done yet */
396 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
398 tag = lr->lr_res_msgtype;
399 Debug( LDAP_DEBUG_ANY, "request %ld done\n",
401 Debug( LDAP_DEBUG_TRACE,
402 "res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
403 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
404 lr->lr_res_matched ? lr->lr_res_matched : "" );
405 if ( !simple_request ) {
406 if ( ber.ber_buf != NULL ) {
407 free( ber.ber_buf ); /* gack! */
410 if ( build_result_ber( ld, &ber, lr )
412 ld->ld_errno = LDAP_NO_MEMORY;
413 rc = -1; /* fatal error */
417 ldap_free_request( ld, lr );
421 ldap_free_connection( ld, lc, 0, 1 );
426 if ( ber.ber_buf == NULL ) {
430 #endif /* LDAP_REFERRALS */
431 /* make a new ldap message */
432 if ( (new = (LDAPMessage *) calloc( 1, sizeof(LDAPMessage) ))
434 ld->ld_errno = LDAP_NO_MEMORY;
437 new->lm_msgid = (int)id;
438 new->lm_msgtype = tag;
439 new->lm_ber = ber_dup( &ber );
442 if ( ld->ld_cache != NULL ) {
443 ldap_add_result_to_cache( ld, new );
445 #endif /* LDAP_NOCACHE */
447 /* is this the one we're looking for? */
448 if ( msgid == LDAP_RES_ANY || id == msgid ) {
450 || (new->lm_msgtype != LDAP_RES_SEARCH_RESULT
451 && new->lm_msgtype != LDAP_RES_SEARCH_ENTRY) ) {
453 ld->ld_errno = LDAP_SUCCESS;
455 } else if ( new->lm_msgtype == LDAP_RES_SEARCH_RESULT) {
456 foundit = 1; /* return the chain later */
461 * if not, we must add it to the list of responses. if
462 * the msgid is already there, it must be part of an existing
467 for ( l = ld->ld_responses; l != NULLMSG; l = l->lm_next ) {
468 if ( l->lm_msgid == new->lm_msgid )
473 /* not part of an existing search response */
474 if ( l == NULLMSG ) {
477 ld->ld_errno = LDAP_SUCCESS;
481 new->lm_next = ld->ld_responses;
482 ld->ld_responses = new;
483 return( -2 ); /* continue looking */
486 Debug( LDAP_DEBUG_TRACE, "adding response id %d type %d:\n",
487 new->lm_msgid, new->lm_msgtype, 0 );
489 /* part of a search response - add to end of list of entries */
490 for ( tmp = l; tmp->lm_chain != NULLMSG &&
491 tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY;
492 tmp = tmp->lm_chain )
496 /* return the whole chain if that's what we were looking for */
498 if ( prev == NULLMSG )
499 ld->ld_responses = l->lm_next;
501 prev->lm_next = l->lm_next;
503 ld->ld_errno = LDAP_SUCCESS;
504 #ifdef LDAP_WORLD_P16
506 * XXX questionable fix; see text for [P16] on
507 * http://www.critical-angle.com/ldapworld/patch/
509 * inclusion of this patch causes searchs to hang on
512 return( l->lm_msgtype );
513 #else /* LDAP_WORLD_P16 */
515 #endif /* !LDAP_WORLD_P16 */
518 return( -2 ); /* continue looking */
522 #ifdef LDAP_REFERRALS
524 build_result_ber( LDAP *ld, BerElement *ber, LDAPRequest *lr )
530 ldap_set_ber_options( ld, ber );
531 if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
532 (long)lr->lr_res_msgtype, lr->lr_res_errno,
533 lr->lr_res_matched ? lr->lr_res_matched : "",
534 lr->lr_res_error ? lr->lr_res_error : "" ) == LBER_ERROR ) {
535 return( LBER_ERROR );
539 if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) {
540 return( LBER_ERROR );
543 if ( ber_get_int( ber, &along ) == LBER_ERROR ) {
544 return( LBER_ERROR );
547 return( ber_peek_tag( ber, &len ));
552 merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
555 * Merge error information in "lr" with "parentr" error code and string.
557 if ( lr->lr_res_errno == LDAP_PARTIAL_RESULTS ) {
558 parentr->lr_res_errno = lr->lr_res_errno;
559 if ( lr->lr_res_error != NULL ) {
560 (void)ldap_append_referral( ld, &parentr->lr_res_error,
563 } else if ( lr->lr_res_errno != LDAP_SUCCESS &&
564 parentr->lr_res_errno == LDAP_SUCCESS ) {
565 parentr->lr_res_errno = lr->lr_res_errno;
566 if ( parentr->lr_res_error != NULL ) {
567 free( parentr->lr_res_error );
569 parentr->lr_res_error = lr->lr_res_error;
570 lr->lr_res_error = NULL;
571 if ( NAME_ERROR( lr->lr_res_errno )) {
572 if ( parentr->lr_res_matched != NULL ) {
573 free( parentr->lr_res_matched );
575 parentr->lr_res_matched = lr->lr_res_matched;
576 lr->lr_res_matched = NULL;
580 Debug( LDAP_DEBUG_TRACE, "merged parent (id %d) error info: ",
581 parentr->lr_msgid, 0, 0 );
582 Debug( LDAP_DEBUG_TRACE, "result errno %d, error <%s>, matched <%s>\n",
583 parentr->lr_res_errno, parentr->lr_res_error ?
584 parentr->lr_res_error : "", parentr->lr_res_matched ?
585 parentr->lr_res_matched : "" );
587 #endif /* LDAP_REFERRALS */
591 #if defined( LDAP_CONNECTIONLESS ) || !defined( LDAP_REFERRALS )
592 #if !defined( MACOS ) && !defined( DOS ) && !defined( _WIN32 )
594 ldap_select1( LDAP *ld, struct timeval *timeout )
599 if ( tblsize == 0 ) {
601 tblsize = sysconf( _SC_OPEN_MAX );
602 #elif HAVE_GETDTABLESIZE
603 tblsize = getdtablesize();
605 tblsize = FD_SETSIZE;
608 if ( tblsize > FD_SETSIZE ) {
609 tblsize = FD_SETSIZE;
611 #endif /* FD_SETSIZE */
615 FD_SET( ld->ld_sb.sb_sd, &readfds );
617 return( select( tblsize, &readfds, 0, 0, timeout ) );
624 ldap_select1( LDAP *ld, struct timeval *timeout )
626 return( tcpselect( ld->ld_sb.sb_sd, timeout ));
631 #if ( defined( DOS ) && defined( WINSOCK )) || defined( _WIN32 )
633 ldap_select1( LDAP *ld, struct timeval *timeout )
639 FD_SET( ld->ld_sb.sb_sd, &readfds );
641 rc = select( 1, &readfds, 0, 0, timeout );
642 return( rc == SOCKET_ERROR ? -1 : rc );
644 #endif /* WINSOCK || _WIN32 */
650 ldap_select1( LDAP *ld, struct timeval *timeout )
656 FD_SET( ld->ld_sb.sb_sd, &readfds );
658 res = select( FD_SETSIZE, &readfds, NULL, NULL, timeout );
659 if ( res == -1 && errno == EINTR) {
660 /* We've been CTRL-C'ed at this point. It'd be nice to
661 carry on but PC-NFS currently won't let us! */
662 printf("\n*** CTRL-C ***\n");
671 ldap_select1( LDAP *ld, struct timeval *timeout )
676 if ( timeout != NULL ) {
677 endtime = timeout->tv_sec * CLK_TCK +
678 timeout->tv_usec * CLK_TCK / 1000000 + clock();
683 rc = netqlen( ld->ld_sb.sb_sd );
684 } while ( rc <= 0 && ( timeout == NULL || clock() < endtime ));
686 return( rc > 0 ? 1 : 0 );
690 #endif /* !LDAP_REFERRALS */
694 ldap_msgfree( LDAPMessage *lm )
699 Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
701 for ( ; lm != NULLMSG; lm = next ) {
703 type = lm->lm_msgtype;
704 ber_free( lm->lm_ber, 1 );
712 * ldap_msgdelete - delete a message. It returns:
713 * 0 if the entire message was deleted
714 * -1 if the message was not found, or only part of it was found
717 ldap_msgdelete( LDAP *ld, int msgid )
719 LDAPMessage *lm, *prev;
721 Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete\n", 0, 0, 0 );
724 for ( lm = ld->ld_responses; lm != NULLMSG; lm = lm->lm_next ) {
725 if ( lm->lm_msgid == msgid )
733 if ( prev == NULLMSG )
734 ld->ld_responses = lm->lm_next;
736 prev->lm_next = lm->lm_next;
738 if ( ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY )
746 * return 1 if message msgid is waiting to be abandoned, 0 otherwise
749 ldap_abandoned( LDAP *ld, int msgid )
753 if ( ld->ld_abandoned == NULL )
756 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
757 if ( ld->ld_abandoned[i] == msgid )
765 ldap_mark_abandoned( LDAP *ld, int msgid )
769 if ( ld->ld_abandoned == NULL )
772 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
773 if ( ld->ld_abandoned[i] == msgid )
776 if ( ld->ld_abandoned[i] == -1 )
779 for ( ; ld->ld_abandoned[i] != -1; i++ ) {
780 ld->ld_abandoned[i] = ld->ld_abandoned[i + 1];
787 #ifdef LDAP_CONNECTIONLESS
789 cldap_getmsg( LDAP *ld, struct timeval *timeout, BerElement *ber )
792 unsigned long tag, len;
794 if ( ld->ld_sb.sb_ber.ber_ptr >= ld->ld_sb.sb_ber.ber_end ) {
795 rc = ldap_select1( ld, timeout );
796 if ( rc == -1 || rc == 0 ) {
797 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
803 /* get the next message */
804 if ( (tag = ber_get_next( &ld->ld_sb, &len, ber ))
805 != LDAP_TAG_MESSAGE ) {
806 ld->ld_errno = (tag == LBER_DEFAULT ? LDAP_SERVER_DOWN :
813 #endif /* LDAP_CONNECTIONLESS */