2 * Copyright (c) 1990 Regents of the University of Michigan.
5 * result.c - wait for an ldap result
9 static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
19 #if defined( DOS ) || defined( _WIN32 )
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <sys/errno.h>
36 #include <sys/select.h>
42 #include "ucx_select.h"
50 #endif /* USE_SYSCONF */
53 static int ldap_abandoned( LDAP *ld, int msgid );
54 static int ldap_mark_abandoned( LDAP *ld, int msgid );
55 static int wait4msg( LDAP *ld, int msgid, int all, struct timeval *timeout,
56 LDAPMessage **result );
58 static int read1msg( LDAP *ld, int msgid, int all, Sockbuf *sb, LDAPConn *lc,
59 LDAPMessage **result );
60 static int build_result_ber( LDAP *ld, BerElement *ber, LDAPRequest *lr );
61 static void merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr );
62 #else /* LDAP_REFERRALS */
63 static int read1msg( LDAP *ld, int msgid, int all, Sockbuf *sb,
64 LDAPMessage **result );
65 #endif /* LDAP_REFERRALS */
66 #if defined( CLDAP ) || !defined( LDAP_REFERRALS )
67 static int ldap_select1( LDAP *ld, struct timeval *timeout );
69 #else /* NEEDPROTOS */
70 static int ldap_abandoned();
71 static int ldap_mark_abandoned();
72 static int wait4msg();
73 static int read1msg();
75 static int build_result_ber();
76 static void merge_error_info();
77 #endif /* LDAP_REFERRALS */
78 #if defined( CLDAP ) || !defined( LDAP_REFERRALS )
79 static int ldap_select1();
81 #endif /* NEEDPROTOS */
83 #if !defined( MACOS ) && !defined( DOS )
89 * ldap_result - wait for an ldap result response to a message from the
90 * ldap server. If msgid is -1, any message will be accepted, otherwise
91 * ldap_result will wait for a response with msgid. If all is 0 the
92 * first message with id msgid will be accepted, otherwise, ldap_result
93 * will wait for all responses with id msgid and then return a pointer to
94 * the entire list of messages. This is only useful for search responses,
95 * which can be of two message types (zero or more entries, followed by an
96 * ldap result). The type of the first message received is returned.
97 * When waiting, any messages that have been abandoned are discarded.
100 * ldap_result( s, msgid, all, timeout, result )
103 ldap_result( LDAP *ld, int msgid, int all, struct timeval *timeout,
104 LDAPMessage **result )
106 LDAPMessage *lm, *lastlm, *nextlm;
109 * First, look through the list of responses we have received on
110 * this association and see if the response we're interested in
111 * is there. If it is, return it. If not, call wait4msg() to
112 * wait until it arrives or timeout occurs.
115 Debug( LDAP_DEBUG_TRACE, "ldap_result\n", 0, 0, 0 );
119 for ( lm = ld->ld_responses; lm != NULLMSG; lm = nextlm ) {
120 nextlm = lm->lm_next;
122 if ( ldap_abandoned( ld, lm->lm_msgid ) ) {
123 ldap_mark_abandoned( ld, lm->lm_msgid );
125 if ( lastlm == NULLMSG ) {
126 ld->ld_responses = lm->lm_next;
128 lastlm->lm_next = nextlm;
136 if ( msgid == LDAP_RES_ANY || lm->lm_msgid == msgid ) {
140 || (lm->lm_msgtype != LDAP_RES_SEARCH_RESULT
141 && lm->lm_msgtype != LDAP_RES_SEARCH_ENTRY) )
144 for ( tmp = lm; tmp != NULLMSG; tmp = tmp->lm_chain ) {
145 if ( tmp->lm_msgtype == LDAP_RES_SEARCH_RESULT )
149 if ( tmp == NULLMSG ) {
150 return( wait4msg( ld, msgid, all, timeout,
158 if ( lm == NULLMSG ) {
159 return( wait4msg( ld, msgid, all, timeout, result ) );
162 if ( lastlm == NULLMSG ) {
163 ld->ld_responses = (all == 0 && lm->lm_chain != NULLMSG
164 ? lm->lm_chain : lm->lm_next);
166 lastlm->lm_next = (all == 0 && lm->lm_chain != NULLMSG
167 ? lm->lm_chain : lm->lm_next);
170 lm->lm_chain = NULLMSG;
171 lm->lm_next = NULLMSG;
174 ld->ld_errno = LDAP_SUCCESS;
175 return( lm->lm_msgtype );
179 wait4msg( LDAP *ld, int msgid, int all, struct timeval *timeout,
180 LDAPMessage **result )
183 struct timeval tv, *tvp;
184 long start_time, tmp_time;
185 #ifdef LDAP_REFERRALS
186 LDAPConn *lc, *nextlc;
187 #endif /* LDAP_REFERRALS */
190 if ( timeout == NULL ) {
191 Debug( LDAP_DEBUG_TRACE, "wait4msg (infinite timeout)\n",
194 Debug( LDAP_DEBUG_TRACE, "wait4msg (timeout %ld sec, %ld usec)\n",
195 timeout->tv_sec, timeout->tv_usec, 0 );
197 #endif /* LDAP_DEBUG */
199 if ( timeout == NULL ) {
204 start_time = (long)time( NULL );
209 #ifndef LDAP_REFERRALS
211 if ( ld->ld_sb.sb_ber.ber_ptr >= ld->ld_sb.sb_ber.ber_end ) {
212 rc = ldap_select1( ld, tvp );
214 #if !defined( MACOS ) && !defined( DOS )
215 if ( rc == 0 || ( rc == -1 && (( ld->ld_options &
216 LDAP_OPT_RESTART ) == 0 || errno != EINTR ))) {
218 if ( rc == -1 || rc == 0 ) {
220 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
227 rc = -2; /* select interrupted: loop */
229 rc = read1msg( ld, msgid, all, &ld->ld_sb, result );
231 #else /* !LDAP_REFERRALS */
233 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
234 ldap_dump_connection( ld, ld->ld_conns, 1 );
235 ldap_dump_requests_and_responses( ld );
237 #endif /* LDAP_DEBUG */
238 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
239 if ( lc->lconn_sb->sb_ber.ber_ptr <
240 lc->lconn_sb->sb_ber.ber_end ) {
241 rc = read1msg( ld, msgid, all, lc->lconn_sb,
248 rc = do_ldap_select( ld, tvp );
251 #if defined( LDAP_DEBUG ) && !defined( MACOS ) && !defined( DOS )
253 Debug( LDAP_DEBUG_TRACE,
254 "do_ldap_select returned -1: errno %d\n",
259 #if !defined( MACOS ) && !defined( DOS )
260 if ( rc == 0 || ( rc == -1 && (( ld->ld_options &
261 LDAP_OPT_RESTART ) == 0 || errno != EINTR ))) {
263 if ( rc == -1 || rc == 0 ) {
265 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
271 rc = -2; /* select interrupted: loop */
274 for ( lc = ld->ld_conns; rc == -2 && lc != NULL;
276 nextlc = lc->lconn_next;
277 if ( lc->lconn_status ==
278 LDAP_CONNST_CONNECTED &&
279 ldap_is_read_ready( ld,
281 rc = read1msg( ld, msgid, all,
282 lc->lconn_sb, lc, result );
287 #endif /* !LDAP_REFERRALS */
289 if ( rc == -2 && tvp != NULL ) {
290 tmp_time = (long)time( NULL );
291 if (( tv.tv_sec -= ( tmp_time - start_time )) <= 0 ) {
292 rc = 0; /* timed out */
293 ld->ld_errno = LDAP_TIMEOUT;
297 Debug( LDAP_DEBUG_TRACE, "wait4msg: %ld secs to go\n",
299 start_time = tmp_time;
308 read1msg( LDAP *ld, int msgid, int all, Sockbuf *sb,
309 #ifdef LDAP_REFERRALS
311 #endif /* LDAP_REFERRALS */
312 LDAPMessage **result )
315 LDAPMessage *new, *l, *prev, *tmp;
317 unsigned long tag, len;
319 #ifdef LDAP_REFERRALS
322 int rc, refer_cnt, hadref, simple_request;
324 #endif /* LDAP_REFERRALS */
326 Debug( LDAP_DEBUG_TRACE, "read1msg\n", 0, 0, 0 );
329 ldap_set_ber_options( ld, &ber );
331 /* get the next message */
332 if ( (tag = ber_get_next( sb, &len, &ber ))
333 != LDAP_TAG_MESSAGE ) {
334 ld->ld_errno = (tag == LBER_DEFAULT ? LDAP_SERVER_DOWN :
340 if ( ber_get_int( &ber, &id ) == LBER_ERROR ) {
341 ld->ld_errno = LDAP_DECODING_ERROR;
345 /* if it's been abandoned, toss it */
346 if ( ldap_abandoned( ld, (int)id ) ) {
347 free( ber.ber_buf ); /* gack! */
348 return( -2 ); /* continue looking */
351 #ifdef LDAP_REFERRALS
352 if (( lr = ldap_find_request_by_msgid( ld, id )) == NULL ) {
353 Debug( LDAP_DEBUG_ANY,
354 "no request for response with msgid %ld (tossing)\n",
356 free( ber.ber_buf ); /* gack! */
357 return( -2 ); /* continue looking */
359 Debug( LDAP_DEBUG_TRACE, "got %s msgid %ld, original id %d\n",
360 ( tag == LDAP_RES_SEARCH_ENTRY ) ? "entry" : "result", id,
363 #endif /* LDAP_REFERRALS */
365 /* the message type */
366 if ( (tag = ber_peek_tag( &ber, &len )) == LBER_ERROR ) {
367 ld->ld_errno = LDAP_DECODING_ERROR;
371 #ifdef LDAP_REFERRALS
373 hadref = simple_request = 0;
374 rc = -2; /* default is to keep looking (no response found) */
375 lr->lr_res_msgtype = tag;
377 if ( tag != LDAP_RES_SEARCH_ENTRY ) {
378 if ( ld->ld_version >= LDAP_VERSION2 &&
379 ( lr->lr_parent != NULL ||
380 ( ld->ld_options & LDAP_OPT_REFERRALS ) != 0 )) {
381 tmpber = ber; /* struct copy */
382 if ( ber_scanf( &tmpber, "{iaa}", &lderr,
383 &lr->lr_res_matched, &lr->lr_res_error )
385 if ( lderr != LDAP_SUCCESS ) {
386 /* referrals are in error string */
387 refer_cnt = ldap_chase_referrals( ld, lr,
388 &lr->lr_res_error, &hadref );
391 /* save errno, message, and matched string */
392 if ( !hadref || lr->lr_res_error == NULL ) {
393 lr->lr_res_errno = ( lderr ==
394 LDAP_PARTIAL_RESULTS ) ? LDAP_SUCCESS
396 } else if ( ld->ld_errno != LDAP_SUCCESS ) {
397 lr->lr_res_errno = ld->ld_errno;
399 lr->lr_res_errno = LDAP_PARTIAL_RESULTS;
401 Debug( LDAP_DEBUG_TRACE,
402 "new result: 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 : "" );
408 Debug( LDAP_DEBUG_TRACE,
409 "read1msg: %d new referrals\n", refer_cnt, 0, 0 );
411 if ( refer_cnt != 0 ) { /* chasing referrals */
412 free( ber.ber_buf ); /* gack! */
414 if ( refer_cnt < 0 ) {
415 return( -1 ); /* fatal error */
417 lr->lr_status = LDAP_REQST_CHASINGREFS;
419 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
420 /* request without any referrals */
421 simple_request = ( hadref ? 0 : 1 );
423 /* request with referrals or child request */
424 free( ber.ber_buf ); /* gack! */
428 while ( lr->lr_parent != NULL ) {
429 merge_error_info( ld, lr->lr_parent, lr );
432 if ( --lr->lr_outrefcnt > 0 ) {
433 break; /* not completely done yet */
437 if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
439 tag = lr->lr_res_msgtype;
440 Debug( LDAP_DEBUG_ANY, "request %ld done\n",
442 Debug( LDAP_DEBUG_TRACE,
443 "res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
444 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
445 lr->lr_res_matched ? lr->lr_res_matched : "" );
446 if ( !simple_request ) {
447 if ( ber.ber_buf != NULL ) {
448 free( ber.ber_buf ); /* gack! */
451 if ( build_result_ber( ld, &ber, lr )
453 ld->ld_errno = LDAP_NO_MEMORY;
454 rc = -1; /* fatal error */
458 ldap_free_request( ld, lr );
462 ldap_free_connection( ld, lc, 0, 1 );
467 if ( ber.ber_buf == NULL ) {
471 #endif /* LDAP_REFERRALS */
472 /* make a new ldap message */
473 if ( (new = (LDAPMessage *) calloc( 1, sizeof(LDAPMessage) ))
475 ld->ld_errno = LDAP_NO_MEMORY;
478 new->lm_msgid = (int)id;
479 new->lm_msgtype = tag;
480 new->lm_ber = ber_dup( &ber );
483 if ( ld->ld_cache != NULL ) {
484 ldap_add_result_to_cache( ld, new );
486 #endif /* NO_CACHE */
488 /* is this the one we're looking for? */
489 if ( msgid == LDAP_RES_ANY || id == msgid ) {
491 || (new->lm_msgtype != LDAP_RES_SEARCH_RESULT
492 && new->lm_msgtype != LDAP_RES_SEARCH_ENTRY) ) {
494 ld->ld_errno = LDAP_SUCCESS;
496 } else if ( new->lm_msgtype == LDAP_RES_SEARCH_RESULT) {
497 foundit = 1; /* return the chain later */
502 * if not, we must add it to the list of responses. if
503 * the msgid is already there, it must be part of an existing
508 for ( l = ld->ld_responses; l != NULLMSG; l = l->lm_next ) {
509 if ( l->lm_msgid == new->lm_msgid )
514 /* not part of an existing search response */
515 if ( l == NULLMSG ) {
518 ld->ld_errno = LDAP_SUCCESS;
522 new->lm_next = ld->ld_responses;
523 ld->ld_responses = new;
524 return( -2 ); /* continue looking */
527 Debug( LDAP_DEBUG_TRACE, "adding response id %d type %d:\n",
528 new->lm_msgid, new->lm_msgtype, 0 );
530 /* part of a search response - add to end of list of entries */
531 for ( tmp = l; tmp->lm_chain != NULLMSG &&
532 tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY;
533 tmp = tmp->lm_chain )
537 /* return the whole chain if that's what we were looking for */
539 if ( prev == NULLMSG )
540 ld->ld_responses = l->lm_next;
542 prev->lm_next = l->lm_next;
544 ld->ld_errno = LDAP_SUCCESS;
545 #ifdef LDAP_WORLD_P16
547 * XXX questionable fix; see text for [P16] on
548 * http://www.critical-angle.com/ldapworld/patch/
550 * inclusion of this patch causes searchs to hang on
553 return( l->lm_msgtype );
554 #else /* LDAP_WORLD_P16 */
556 #endif /* !LDAP_WORLD_P16 */
559 return( -2 ); /* continue looking */
563 #ifdef LDAP_REFERRALS
565 build_result_ber( LDAP *ld, BerElement *ber, LDAPRequest *lr )
571 ldap_set_ber_options( ld, ber );
572 if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
573 (long)lr->lr_res_msgtype, lr->lr_res_errno,
574 lr->lr_res_matched ? lr->lr_res_matched : "",
575 lr->lr_res_error ? lr->lr_res_error : "" ) == LBER_ERROR ) {
576 return( LBER_ERROR );
580 if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) {
581 return( LBER_ERROR );
584 if ( ber_get_int( ber, &along ) == LBER_ERROR ) {
585 return( LBER_ERROR );
588 return( ber_peek_tag( ber, &len ));
593 merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
596 * Merge error information in "lr" with "parentr" error code and string.
598 if ( lr->lr_res_errno == LDAP_PARTIAL_RESULTS ) {
599 parentr->lr_res_errno = lr->lr_res_errno;
600 if ( lr->lr_res_error != NULL ) {
601 (void)ldap_append_referral( ld, &parentr->lr_res_error,
604 } else if ( lr->lr_res_errno != LDAP_SUCCESS &&
605 parentr->lr_res_errno == LDAP_SUCCESS ) {
606 parentr->lr_res_errno = lr->lr_res_errno;
607 if ( parentr->lr_res_error != NULL ) {
608 free( parentr->lr_res_error );
610 parentr->lr_res_error = lr->lr_res_error;
611 lr->lr_res_error = NULL;
612 if ( NAME_ERROR( lr->lr_res_errno )) {
613 if ( parentr->lr_res_matched != NULL ) {
614 free( parentr->lr_res_matched );
616 parentr->lr_res_matched = lr->lr_res_matched;
617 lr->lr_res_matched = NULL;
621 Debug( LDAP_DEBUG_TRACE, "merged parent (id %d) error info: ",
622 parentr->lr_msgid, 0, 0 );
623 Debug( LDAP_DEBUG_TRACE, "result errno %d, error <%s>, matched <%s>\n",
624 parentr->lr_res_errno, parentr->lr_res_error ?
625 parentr->lr_res_error : "", parentr->lr_res_matched ?
626 parentr->lr_res_matched : "" );
628 #endif /* LDAP_REFERRALS */
632 #if defined( CLDAP ) || !defined( LDAP_REFERRALS )
633 #if !defined( MACOS ) && !defined( DOS ) && !defined( _WIN32 )
635 ldap_select1( LDAP *ld, struct timeval *timeout )
640 if ( tblsize == 0 ) {
643 * It is invalid to use a set size in excess of the type
644 * scope, as defined for the fd_set in sys/types.h. This
645 * is true for any OS.
647 tblsize = FD_SETSIZE;
648 #else /* !FD_SETSIZE*/
650 tblsize = sysconf( _SC_OPEN_MAX );
651 #else /* USE_SYSCONF */
652 tblsize = getdtablesize();
653 #endif /* USE_SYSCONF */
654 #endif /* !FD_SETSIZE*/
658 FD_SET( ld->ld_sb.sb_sd, &readfds );
660 return( select( tblsize, &readfds, 0, 0, timeout ) );
667 ldap_select1( LDAP *ld, struct timeval *timeout )
669 return( tcpselect( ld->ld_sb.sb_sd, timeout ));
674 #if ( defined( DOS ) && defined( WINSOCK )) || defined( _WIN32 )
676 ldap_select1( LDAP *ld, struct timeval *timeout )
682 FD_SET( ld->ld_sb.sb_sd, &readfds );
684 rc = select( 1, &readfds, 0, 0, timeout );
685 return( rc == SOCKET_ERROR ? -1 : rc );
687 #endif /* WINSOCK || _WIN32 */
693 ldap_select1( LDAP *ld, struct timeval *timeout )
699 FD_SET( ld->ld_sb.sb_sd, &readfds );
701 res = select( FD_SETSIZE, &readfds, NULL, NULL, timeout );
702 if ( res == -1 && errno == EINTR) {
703 /* We've been CTRL-C'ed at this point. It'd be nice to
704 carry on but PC-NFS currently won't let us! */
705 printf("\n*** CTRL-C ***\n");
714 ldap_select1( LDAP *ld, struct timeval *timeout )
719 if ( timeout != NULL ) {
720 endtime = timeout->tv_sec * CLK_TCK +
721 timeout->tv_usec * CLK_TCK / 1000000 + clock();
726 rc = netqlen( ld->ld_sb.sb_sd );
727 } while ( rc <= 0 && ( timeout == NULL || clock() < endtime ));
729 return( rc > 0 ? 1 : 0 );
733 #endif /* !LDAP_REFERRALS */
737 ldap_msgfree( LDAPMessage *lm )
742 Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
744 for ( ; lm != NULLMSG; lm = next ) {
746 type = lm->lm_msgtype;
747 ber_free( lm->lm_ber, 1 );
755 * ldap_msgdelete - delete a message. It returns:
756 * 0 if the entire message was deleted
757 * -1 if the message was not found, or only part of it was found
760 ldap_msgdelete( LDAP *ld, int msgid )
762 LDAPMessage *lm, *prev;
764 Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete\n", 0, 0, 0 );
767 for ( lm = ld->ld_responses; lm != NULLMSG; lm = lm->lm_next ) {
768 if ( lm->lm_msgid == msgid )
776 if ( prev == NULLMSG )
777 ld->ld_responses = lm->lm_next;
779 prev->lm_next = lm->lm_next;
781 if ( ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY )
789 * return 1 if message msgid is waiting to be abandoned, 0 otherwise
792 ldap_abandoned( LDAP *ld, int msgid )
796 if ( ld->ld_abandoned == NULL )
799 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
800 if ( ld->ld_abandoned[i] == msgid )
808 ldap_mark_abandoned( LDAP *ld, int msgid )
812 if ( ld->ld_abandoned == NULL )
815 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
816 if ( ld->ld_abandoned[i] == msgid )
819 if ( ld->ld_abandoned[i] == -1 )
822 for ( ; ld->ld_abandoned[i] != -1; i++ ) {
823 ld->ld_abandoned[i] = ld->ld_abandoned[i + 1];
832 cldap_getmsg( LDAP *ld, struct timeval *timeout, BerElement *ber )
835 unsigned long tag, len;
837 if ( ld->ld_sb.sb_ber.ber_ptr >= ld->ld_sb.sb_ber.ber_end ) {
838 rc = ldap_select1( ld, timeout );
839 if ( rc == -1 || rc == 0 ) {
840 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
846 /* get the next message */
847 if ( (tag = ber_get_next( &ld->ld_sb, &len, ber ))
848 != LDAP_TAG_MESSAGE ) {
849 ld->ld_errno = (tag == LBER_DEFAULT ? LDAP_SERVER_DOWN :