]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/result.c
Improved ldap_int_strtok. If strtok_r does not exists, it will be worked
[openldap] / libraries / libldap / result.c
index 3f8408df18f7ae7b590de4a83f44a95299fad68c..ef3a86fad3b4ba7b29d235650c5ecb08f3b2809a 100644 (file)
@@ -1,87 +1,43 @@
 /*
+ * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
+ * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+ */
+/*  Portions
  *  Copyright (c) 1990 Regents of the University of Michigan.
  *  All rights reserved.
  *
  *  result.c - wait for an ldap result
  */
 
-#ifndef lint 
-static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
-#endif
+#include "portable.h"
 
 #include <stdio.h>
-#include <string.h>
-#ifdef MACOS
 #include <stdlib.h>
-#include <time.h>
-#include "macos.h"
-#else /* MACOS */
-#if defined( DOS ) || defined( _WIN32 )
-#include <time.h>
-#include "msdos.h"
-#ifdef PCNFS
-#include <tklib.h>
-#include <tk_errno.h>
-#include <bios.h>
-#endif /* PCNFS */
-#ifdef NCSA
-#include "externs.h"
-#endif /* NCSA */
-#else /* DOS */
-#include <sys/time.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <sys/errno.h>
-#ifdef _AIX
-#include <sys/select.h>
-#endif /* _AIX */
-#include "portable.h"
-#endif /* DOS */
-#endif /* MACOS */
-#ifdef VMS
-#include "ucx_select.h"
-#endif
-#include "lber.h"
-#include "ldap.h"
+
+#include <ac/errno.h>
+#include <ac/socket.h>
+#include <ac/string.h>
+#include <ac/time.h>
+#include <ac/unistd.h>
+
 #include "ldap-int.h"
 
-#ifdef USE_SYSCONF
-#include <unistd.h>
-#endif /* USE_SYSCONF */
-
-#ifdef NEEDPROTOS
-static int ldap_abandoned( LDAP *ld, int msgid );
-static int ldap_mark_abandoned( LDAP *ld, int msgid );
-static int wait4msg( LDAP *ld, int msgid, int all, struct timeval *timeout,
-       LDAPMessage **result );
-#ifdef LDAP_REFERRALS
-static int read1msg( LDAP *ld, int msgid, int all, Sockbuf *sb, LDAPConn *lc,
-       LDAPMessage **result );
-static int build_result_ber( LDAP *ld, BerElement *ber, LDAPRequest *lr );
-static void merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr );
-#else /* LDAP_REFERRALS */
-static int read1msg( LDAP *ld, int msgid, int all, Sockbuf *sb,
-       LDAPMessage **result );
-#endif /* LDAP_REFERRALS */
-#if defined( CLDAP ) || !defined( LDAP_REFERRALS )
-static int ldap_select1( LDAP *ld, struct timeval *timeout );
-#endif
-#else /* NEEDPROTOS */
-static int ldap_abandoned();
-static int ldap_mark_abandoned();
-static int wait4msg();
-static int read1msg();
-#ifdef LDAP_REFERRALS
-static int build_result_ber();
-static void merge_error_info();
-#endif /* LDAP_REFERRALS */
-#if defined( CLDAP ) || !defined( LDAP_REFERRALS )
-static int ldap_select1();
-#endif
-#endif /* NEEDPROTOS */
 
-#if !defined( MACOS ) && !defined( DOS )
-extern int     errno;
+static int ldap_abandoned LDAP_P(( LDAP *ld, int msgid ));
+static int ldap_mark_abandoned LDAP_P(( LDAP *ld, int msgid ));
+static int wait4msg LDAP_P(( LDAP *ld, int msgid, int all, struct timeval *timeout,
+       LDAPMessage **result ));
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
+static int read1msg LDAP_P(( LDAP *ld, int msgid, int all, Sockbuf *sb, LDAPConn *lc,
+       LDAPMessage **result ));
+static unsigned long build_result_ber LDAP_P(( LDAP *ld, BerElement *ber, LDAPRequest *lr ));
+static void merge_error_info LDAP_P(( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr ));
+#else /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
+static int read1msg LDAP_P(( LDAP *ld, int msgid, int all, Sockbuf *sb,
+       LDAPMessage **result ));
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
+#if defined( LDAP_CONNECTIONLESS ) || !defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS )
+static int ldap_select1 LDAP_P(( LDAP *ld, struct timeval *timeout ));
 #endif
 
 
@@ -138,6 +94,7 @@ ldap_result( LDAP *ld, int msgid, int all, struct timeval *timeout,
 
                        if ( all == 0
                            || (lm->lm_msgtype != LDAP_RES_SEARCH_RESULT
+                           && lm->lm_msgtype != LDAP_RES_SEARCH_REFERENCE      /* LDAPv3 */
                            && lm->lm_msgtype != LDAP_RES_SEARCH_ENTRY) )
                                break;
 
@@ -181,10 +138,11 @@ wait4msg( LDAP *ld, int msgid, int all, struct timeval *timeout,
 {
        int             rc;
        struct timeval  tv, *tvp;
-       long            start_time, tmp_time;
-#ifdef LDAP_REFERRALS
+       time_t          start_time = 0;
+       time_t          tmp_time;
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
        LDAPConn        *lc, *nextlc;
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 
 #ifdef LDAP_DEBUG
        if ( timeout == NULL ) {
@@ -192,7 +150,7 @@ wait4msg( LDAP *ld, int msgid, int all, struct timeval *timeout,
                    0, 0, 0 );
        } else {
                Debug( LDAP_DEBUG_TRACE, "wait4msg (timeout %ld sec, %ld usec)\n",
-                   timeout->tv_sec, timeout->tv_usec, 0 );
+                      (long) timeout->tv_sec, (long) timeout->tv_usec, 0 );
        }
 #endif /* LDAP_DEBUG */
 
@@ -201,22 +159,20 @@ wait4msg( LDAP *ld, int msgid, int all, struct timeval *timeout,
        } else {
                tv = *timeout;
                tvp = &tv;
-               start_time = (long)time( NULL );
+               start_time = time( NULL );
        }
                    
        rc = -2;
        while ( rc == -2 ) {
-#ifndef LDAP_REFERRALS
+#ifndef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
                /* hack attack */
                if ( ld->ld_sb.sb_ber.ber_ptr >= ld->ld_sb.sb_ber.ber_end ) {
                        rc = ldap_select1( ld, tvp );
 
-#if !defined( MACOS ) && !defined( DOS )
-                       if ( rc == 0 || ( rc == -1 && (( ld->ld_options &
-                           LDAP_OPT_RESTART ) == 0 || errno != EINTR ))) {
-#else
-                       if ( rc == -1 || rc == 0 ) {
-#endif
+                       if ( rc == 0 || ( rc == -1 && (
+                               ( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART)
+                                       == LDAP_OPT_OFF )
+                           || errno != EINTR ))) {
                                ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
                                    LDAP_TIMEOUT);
                                return( rc );
@@ -228,11 +184,11 @@ wait4msg( LDAP *ld, int msgid, int all, struct timeval *timeout,
                } else {
                        rc = read1msg( ld, msgid, all, &ld->ld_sb, result );
                }
-#else /* !LDAP_REFERRALS */
+#else /* !LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 #ifdef LDAP_DEBUG
                if ( ldap_debug & LDAP_DEBUG_TRACE ) {
-                       dump_connection( ld, ld->ld_conns, 1 );
-                       dump_requests_and_responses( ld );
+                       ldap_dump_connection( ld, ld->ld_conns, 1 );
+                       ldap_dump_requests_and_responses( ld );
                }
 #endif /* LDAP_DEBUG */
                for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
@@ -248,7 +204,7 @@ wait4msg( LDAP *ld, int msgid, int all, struct timeval *timeout,
                        rc = do_ldap_select( ld, tvp );
 
 
-#if defined( LDAP_DEBUG ) && !defined( MACOS ) && !defined( DOS )
+#ifdef LDAP_DEBUG
                        if ( rc == -1 ) {
                            Debug( LDAP_DEBUG_TRACE,
                                    "do_ldap_select returned -1: errno %d\n",
@@ -256,12 +212,11 @@ wait4msg( LDAP *ld, int msgid, int all, struct timeval *timeout,
                        }
 #endif
 
-#if !defined( MACOS ) && !defined( DOS )
-                       if ( rc == 0 || ( rc == -1 && (( ld->ld_options &
-                           LDAP_OPT_RESTART ) == 0 || errno != EINTR ))) {
-#else
-                       if ( rc == -1 || rc == 0 ) {
-#endif
+                       if ( rc == 0 || ( rc == -1 && (
+                               ( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART)
+                                       == LDAP_OPT_OFF )
+                               || errno != EINTR )))
+                       {
                                ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
                                    LDAP_TIMEOUT);
                                return( rc );
@@ -276,7 +231,7 @@ wait4msg( LDAP *ld, int msgid, int all, struct timeval *timeout,
                                        nextlc = lc->lconn_next;
                                        if ( lc->lconn_status ==
                                            LDAP_CONNST_CONNECTED &&
-                                           is_read_ready( ld,
+                                           ldap_is_read_ready( ld,
                                            lc->lconn_sb )) {
                                                rc = read1msg( ld, msgid, all,
                                                    lc->lconn_sb, lc, result );
@@ -284,10 +239,10 @@ wait4msg( LDAP *ld, int msgid, int all, struct timeval *timeout,
                                }
                        }
                }
-#endif /* !LDAP_REFERRALS */
+#endif /* !LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 
                if ( rc == -2 && tvp != NULL ) {
-                       tmp_time = (long)time( NULL );
+                       tmp_time = time( NULL );
                        if (( tv.tv_sec -=  ( tmp_time - start_time )) <= 0 ) {
                                rc = 0; /* timed out */
                                ld->ld_errno = LDAP_TIMEOUT;
@@ -295,7 +250,7 @@ wait4msg( LDAP *ld, int msgid, int all, struct timeval *timeout,
                        }
 
                        Debug( LDAP_DEBUG_TRACE, "wait4msg:  %ld secs to go\n",
-                               tv.tv_sec, 0, 0 );
+                              (long) tv.tv_sec, 0, 0 );
                        start_time = tmp_time;
                }
        }
@@ -306,9 +261,9 @@ wait4msg( LDAP *ld, int msgid, int all, struct timeval *timeout,
 
 static int
 read1msg( LDAP *ld, int msgid, int all, Sockbuf *sb,
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
     LDAPConn *lc,
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
     LDAPMessage **result )
 {
        BerElement      ber;
@@ -316,17 +271,17 @@ read1msg( LDAP *ld, int msgid, int all, Sockbuf *sb,
        long            id;
        unsigned long   tag, len;
        int             foundit = 0;
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
        LDAPRequest     *lr;
        BerElement      tmpber;
        int             rc, refer_cnt, hadref, simple_request;
        unsigned long   lderr;
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 
        Debug( LDAP_DEBUG_TRACE, "read1msg\n", 0, 0, 0 );
 
-       ber_init( &ber, 0 );
-       set_ber_options( ld, &ber );
+       ber_init_w_nullc( &ber, 0 );
+       ldap_set_ber_options( ld, &ber );
 
        /* get the next message */
        if ( (tag = ber_get_next( sb, &len, &ber ))
@@ -348,8 +303,8 @@ read1msg( LDAP *ld, int msgid, int all, Sockbuf *sb,
                return( -2 );   /* continue looking */
        }
 
-#ifdef LDAP_REFERRALS
-       if (( lr = find_request_by_msgid( ld, id )) == NULL ) {
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
+       if (( lr = ldap_find_request_by_msgid( ld, id )) == NULL ) {
                Debug( LDAP_DEBUG_ANY,
                    "no request for response with msgid %ld (tossing)\n",
                    id, 0, 0 );
@@ -360,7 +315,7 @@ read1msg( LDAP *ld, int msgid, int all, Sockbuf *sb,
            ( tag == LDAP_RES_SEARCH_ENTRY ) ? "entry" : "result", id,
            lr->lr_origid );
        id = lr->lr_origid;
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 
        /* the message type */
        if ( (tag = ber_peek_tag( &ber, &len )) == LBER_ERROR ) {
@@ -368,7 +323,7 @@ read1msg( LDAP *ld, int msgid, int all, Sockbuf *sb,
                return( -1 );
        }
 
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
        refer_cnt = 0;
        hadref = simple_request = 0;
        rc = -2;        /* default is to keep looking (no response found) */
@@ -376,15 +331,17 @@ read1msg( LDAP *ld, int msgid, int all, Sockbuf *sb,
 
        if ( tag != LDAP_RES_SEARCH_ENTRY ) {
                if ( ld->ld_version >= LDAP_VERSION2 &&
-                           ( lr->lr_parent != NULL ||
-                           ( ld->ld_options & LDAP_OPT_REFERRALS ) != 0 )) {
+                       ( lr->lr_parent != NULL ||
+                       ( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS)
+                               != LDAP_OPT_OFF ) ) )
+               {
                        tmpber = ber;   /* struct copy */
                        if ( ber_scanf( &tmpber, "{iaa}", &lderr,
                            &lr->lr_res_matched, &lr->lr_res_error )
                            != LBER_ERROR ) {
                                if ( lderr != LDAP_SUCCESS ) {
                                        /* referrals are in error string */
-                                       refer_cnt = chase_referrals( ld, lr,
+                                       refer_cnt = ldap_chase_referrals( ld, lr,
                                            &lr->lr_res_error, &hadref );
                                }
 
@@ -455,11 +412,11 @@ lr->lr_res_matched ? lr->lr_res_matched : "" );
                                        }
                                }
 
-                               free_request( ld, lr );
+                               ldap_free_request( ld, lr );
                        }
 
                        if ( lc != NULL ) {
-                               free_connection( ld, lc, 0, 1 );
+                               ldap_free_connection( ld, lc, 0, 1 );
                        }
                }
        }
@@ -468,7 +425,7 @@ lr->lr_res_matched ? lr->lr_res_matched : "" );
                return( rc );
        }
 
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
        /* make a new ldap message */
        if ( (new = (LDAPMessage *) calloc( 1, sizeof(LDAPMessage) ))
            == NULL ) {
@@ -479,11 +436,11 @@ lr->lr_res_matched ? lr->lr_res_matched : "" );
        new->lm_msgtype = tag;
        new->lm_ber = ber_dup( &ber );
 
-#ifndef NO_CACHE
+#ifndef LDAP_NOCACHE
                if ( ld->ld_cache != NULL ) {
-                       add_result_to_cache( ld, new );
+                       ldap_add_result_to_cache( ld, new );
                }
-#endif /* NO_CACHE */
+#endif /* LDAP_NOCACHE */
 
        /* is this the one we're looking for? */
        if ( msgid == LDAP_RES_ANY || id == msgid ) {
@@ -542,26 +499,37 @@ lr->lr_res_matched ? lr->lr_res_matched : "" );
                        prev->lm_next = l->lm_next;
                *result = l;
                ld->ld_errno = LDAP_SUCCESS;
+#ifdef LDAP_WORLD_P16
+               /*
+                * XXX questionable fix; see text for [P16] on
+                * http://www.critical-angle.com/ldapworld/patch/
+                *
+                * inclusion of this patch causes searchs to hang on
+                * multiple platforms
+                */
+               return( l->lm_msgtype );
+#else  /* LDAP_WORLD_P16 */
                return( tag );
+#endif /* !LDAP_WORLD_P16 */
        }
 
        return( -2 );   /* continue looking */
 }
 
 
-#ifdef LDAP_REFERRALS
-static int
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
+static unsigned long
 build_result_ber( LDAP *ld, BerElement *ber, LDAPRequest *lr )
 {
        unsigned long   len;
        long            along;
 
-       ber_init( ber, 0 );
-       set_ber_options( ld, ber );
+       ber_init_w_nullc( ber, 0 );
+       ldap_set_ber_options( ld, ber );
        if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
            (long)lr->lr_res_msgtype, lr->lr_res_errno,
            lr->lr_res_matched ? lr->lr_res_matched : "",
-           lr->lr_res_error ? lr->lr_res_error : "" ) == LBER_ERROR ) {
+           lr->lr_res_error ? lr->lr_res_error : "" ) == -1 ) {
                return( LBER_ERROR );
        }
 
@@ -587,7 +555,7 @@ merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
        if ( lr->lr_res_errno == LDAP_PARTIAL_RESULTS ) {
                parentr->lr_res_errno = lr->lr_res_errno;
                if ( lr->lr_res_error != NULL ) {
-                       (void)append_referral( ld, &parentr->lr_res_error,
+                       (void)ldap_append_referral( ld, &parentr->lr_res_error,
                            lr->lr_res_error );
                }
        } else if ( lr->lr_res_errno != LDAP_SUCCESS &&
@@ -614,12 +582,12 @@ merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
            parentr->lr_res_error : "", parentr->lr_res_matched ?
            parentr->lr_res_matched : "" );
 }
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 
 
 
-#if defined( CLDAP ) || !defined( LDAP_REFERRALS )
-#if !defined( MACOS ) && !defined( DOS ) && !defined( _WIN32 )
+#if defined( LDAP_CONNECTIONLESS ) || !defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS )
+
 static int
 ldap_select1( LDAP *ld, struct timeval *timeout )
 {
@@ -627,11 +595,18 @@ ldap_select1( LDAP *ld, struct timeval *timeout )
        static int      tblsize;
 
        if ( tblsize == 0 ) {
-#ifdef USE_SYSCONF
+#ifdef HAVE_SYSCONF
                tblsize = sysconf( _SC_OPEN_MAX );
-#else /* USE_SYSCONF */
+#elif HAVE_GETDTABLESIZE
                tblsize = getdtablesize();
-#endif /* USE_SYSCONF */
+#else
+               tblsize = FD_SETSIZE;
+#endif
+#ifdef FD_SETSIZE
+               if ( tblsize > FD_SETSIZE ) {
+                       tblsize = FD_SETSIZE;
+               }
+#endif /* FD_SETSIZE */
        }
 
        FD_ZERO( &readfds );
@@ -639,78 +614,21 @@ ldap_select1( LDAP *ld, struct timeval *timeout )
 
        return( select( tblsize, &readfds, 0, 0, timeout ) );
 }
-#endif /* !MACOS */
-
 
-#ifdef MACOS
-static int
-ldap_select1( LDAP *ld, struct timeval *timeout )
-{
-       return( tcpselect( ld->ld_sb.sb_sd, timeout ));
-}
-#endif /* MACOS */
+#endif /* !LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 
 
-#if ( defined( DOS ) && defined( WINSOCK )) || defined( _WIN32 )
-static int
-ldap_select1( LDAP *ld, struct timeval *timeout )
+int
+ldap_msgtype( LDAPMessage *lm )
 {
-    fd_set          readfds;
-    int             rc;
-
-    FD_ZERO( &readfds );
-    FD_SET( ld->ld_sb.sb_sd, &readfds );
-
-    rc = select( 1, &readfds, 0, 0, timeout );
-    return( rc == SOCKET_ERROR ? -1 : rc );
+       return( lm ? lm->lm_msgtype : -1 );
 }
-#endif /* WINSOCK || _WIN32 */
-
 
-#ifdef DOS
-#ifdef PCNFS
-static int
-ldap_select1( LDAP *ld, struct timeval *timeout )
-{
-       fd_set  readfds;
-       int     res;
-
-       FD_ZERO( &readfds );
-       FD_SET( ld->ld_sb.sb_sd, &readfds );
-
-       res = select( FD_SETSIZE, &readfds, NULL, NULL, timeout );
-       if ( res == -1 && errno == EINTR) {
-               /* We've been CTRL-C'ed at this point.  It'd be nice to
-                  carry on but PC-NFS currently won't let us! */
-               printf("\n*** CTRL-C ***\n");
-               exit(-1);
-       }
-       return( res );
-}
-#endif /* PCNFS */
-
-#ifdef NCSA
-static int
-ldap_select1( LDAP *ld, struct timeval *timeout )
+int
+ldap_msgid( LDAPMessage *lm )
 {
-       int rc;
-       clock_t endtime;
-
-       if ( timeout != NULL ) {
-               endtime = timeout->tv_sec * CLK_TCK +
-                       timeout->tv_usec * CLK_TCK / 1000000 + clock();
-       }
-
-       do {
-               Stask();
-               rc = netqlen( ld->ld_sb.sb_sd );
-       } while ( rc <= 0 && ( timeout == NULL || clock() < endtime ));
-
-       return( rc > 0 ? 1 : 0 );
+       return( lm ? lm->lm_msgid : -1 );
 }
-#endif /* NCSA */
-#endif /* DOS */
-#endif /* !LDAP_REFERRALS */
 
 
 int
@@ -807,7 +725,7 @@ ldap_mark_abandoned( LDAP *ld, int msgid )
 }
 
 
-#ifdef CLDAP
+#ifdef LDAP_CONNECTIONLESS
 int
 cldap_getmsg( LDAP *ld, struct timeval *timeout, BerElement *ber )
 {
@@ -833,4 +751,4 @@ cldap_getmsg( LDAP *ld, struct timeval *timeout, BerElement *ber )
 
        return( tag );
 }
-#endif /* CLDAP */
+#endif /* LDAP_CONNECTIONLESS */