]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/test.c
Add reference to ldap.conf(5).
[openldap] / libraries / libldap / test.c
index 84f478da80032c7f52f748f6358cfe7c66f6c0ed..cbcf189e4419c86fae156891ff9ff45c301b8bca 100644 (file)
@@ -1,57 +1,53 @@
+/*
+ * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
+ * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+ */
+
+#include "portable.h"
+
 #include <stdio.h>
-#include <ctype.h>
-#include <string.h>
-#ifdef MACOS
 #include <stdlib.h>
-#ifdef THINK_C
-#include <console.h>
-#include <unix.h>
-#include <fcntl.h>
-#endif /* THINK_C */
-#include "macos.h"
-#else /* MACOS */
-#if defined( DOS ) || defined( _WIN32 )
-#include "msdos.h"
-#if defined( WINSOCK ) || defined( _WIN32 )
-#include "console.h"
-#endif /* WINSOCK */
-#else /* DOS */
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <sys/time.h>
+
+#include <ac/ctype.h>
+#include <ac/socket.h>
+#include <ac/string.h>
+#include <ac/time.h>
+#include <ac/unistd.h>
+
 #include <sys/stat.h>
+
+#ifdef HAVE_SYS_FILE_H
 #include <sys/file.h>
-#ifndef VMS
+#endif
+#ifdef HAVE_IO_H
+#include <io.h>
+#endif
+
 #include <fcntl.h>
-#include <unistd.h>
-#endif /* VMS */
-#endif /* DOS */
-#endif /* MACOS */
-
-#include "lber.h"
-#include "ldap.h"
-
-#if !defined( PCNFS ) && !defined( WINSOCK ) && !defined( MACOS )
-#define MOD_USE_BVALS
-#endif /* !PCNFS && !WINSOCK && !MACOS */
-
-#ifdef NEEDPROTOS
-static void handle_result( LDAP *ld, LDAPMessage *lm );
-static void print_ldap_result( LDAP *ld, LDAPMessage *lm, char *s );
-static void print_search_entry( LDAP *ld, LDAPMessage *res );
-static void free_list( char **list );
-#else
-static void handle_result();
-static void print_ldap_result();
-static void print_search_entry();
-static void free_list();
-#endif /* NEEDPROTOS */
 
-#define NOCACHEERRMSG  "don't compile with -DNO_CACHE if you desire local caching"
+/* including the "internal" defs is legit and nec. since this test routine has 
+ * a-priori knowledge of libldap internal workings.
+ * hodges@stanford.edu 5-Feb-96
+ */
+#include "ldap-int.h"
+
+/* local functions */
+#ifndef HAVE_GETLINE
+static char *getline LDAP_P(( char *line, int len, FILE *fp, char *prompt ));
+#endif
+static char **get_list LDAP_P(( char *prompt ));
+static int file_read LDAP_P(( char *path, struct berval *bv ));
+static LDAPMod **get_modlist LDAP_P(( char *prompt1, char *prompt2, char *prompt3 ));
+static void handle_result LDAP_P(( LDAP *ld, LDAPMessage *lm ));
+static void print_ldap_result LDAP_P(( LDAP *ld, LDAPMessage *lm, char *s ));
+static void print_search_entry LDAP_P(( LDAP *ld, LDAPMessage *res ));
+static void free_list LDAP_P(( char **list ));
 
-char *dnsuffix;
+#define NOCACHEERRMSG  "don't compile with -DLDAP_NOCACHE if you desire local caching"
 
-#ifndef WINSOCK
+static char *dnsuffix;
+
+#ifndef HAVE_GETLINE
 static char *
 getline( char *line, int len, FILE *fp, char *prompt )
 {
@@ -64,7 +60,7 @@ getline( char *line, int len, FILE *fp, char *prompt )
 
        return( line );
 }
-#endif /* WINSOCK */
+#endif
 
 static char **
 get_list( char *prompt )
@@ -112,7 +108,6 @@ free_list( char **list )
 }
 
 
-#ifdef MOD_USE_BVALS
 static int
 file_read( char *path, struct berval *bv )
 {
@@ -149,7 +144,7 @@ file_read( char *path, struct berval *bv )
        eof = feof( fp );
        fclose( fp );
 
-       if ( rlen != bv->bv_len ) {
+       if ( (unsigned long) rlen != bv->bv_len ) {
                perror( path );
                free( bv->bv_val );
                return( -1 );
@@ -157,7 +152,6 @@ file_read( char *path, struct berval *bv )
 
        return( bv->bv_len );
 }
-#endif /* MOD_USE_BVALS */
 
 
 static LDAPMod **
@@ -167,9 +161,7 @@ get_modlist( char *prompt1, char *prompt2, char *prompt3 )
        int             num;
        LDAPMod         tmp;
        LDAPMod         **result;
-#ifdef MOD_USE_BVALS
        struct berval   **bvals;
-#endif /* MOD_USE_BVALS */
 
        num = 0;
        result = NULL;
@@ -188,7 +180,7 @@ get_modlist( char *prompt1, char *prompt2, char *prompt3 )
                tmp.mod_type = strdup( buf );
 
                tmp.mod_values = get_list( prompt3 );
-#ifdef MOD_USE_BVALS
+
                if ( tmp.mod_values != NULL ) {
                        int     i;
 
@@ -214,7 +206,6 @@ get_modlist( char *prompt1, char *prompt2, char *prompt3 )
                        tmp.mod_bvalues = bvals;
                        tmp.mod_op |= LDAP_MOD_BVALUES;
                }
-#endif /* MOD_USE_BVALS */
 
                if ( result == NULL )
                        result = (LDAPMod **) malloc( sizeof(LDAPMod *) );
@@ -235,15 +226,15 @@ get_modlist( char *prompt1, char *prompt2, char *prompt3 )
 }
 
 
-#ifdef LDAP_REFERRALS
-int
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
+static int
 bind_prompt( LDAP *ld, char **dnp, char **passwdp, int *authmethodp,
        int freeit )
 {
        static char     dn[256], passwd[256];
 
        if ( !freeit ) {
-#ifdef KERBEROS
+#ifdef HAVE_KERBEROS
                getline( dn, sizeof(dn), stdin,
                    "re-bind method (0->simple, 1->krbv41, 2->krbv42, 3->krbv41&2)? " );
                if (( *authmethodp = atoi( dn )) == 3 ) {
@@ -251,9 +242,9 @@ bind_prompt( LDAP *ld, char **dnp, char **passwdp, int *authmethodp,
                } else {
                        *authmethodp |= 0x80;
                }
-#else /* KERBEROS */
+#else /* HAVE_KERBEROS */
                *authmethodp = LDAP_AUTH_SIMPLE;
-#endif /* KERBEROS */
+#endif /* HAVE_KERBEROS */
 
                getline( dn, sizeof(dn), stdin, "re-bind dn? " );
                strcat( dn, dnsuffix );
@@ -270,18 +261,13 @@ bind_prompt( LDAP *ld, char **dnp, char **passwdp, int *authmethodp,
 
        return( LDAP_SUCCESS );
 }
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 
 
 int
-#ifdef WINSOCK
-ldapmain(
-#else /* WINSOCK */
-main(
-#endif /* WINSOCK */
-       int argc, char **argv )
+main( int argc, char **argv )
 {
-       LDAP            *ld;
+       LDAP            *ld = NULL;
        int             i, c, port, cldapflg, errflg, method, id, msgtype;
        char            line[256], command1, command2, command3;
        char            passwd[64], dn[256], rdn[64], attr[64], value[256];
@@ -296,18 +282,6 @@ main(
        int             copyoptions = 0;
        LDAPURLDesc     *ludp;
 
-       extern char     *optarg;
-       extern int      optind;
-
-#ifdef MACOS
-       if (( argv = get_list( "cmd line arg?" )) == NULL ) {
-               exit( 1 );
-       }
-       for ( argc = 0; argv[ argc ] != NULL; ++argc ) {
-               ;
-       }
-#endif /* MACOS */
-
        host = NULL;
        port = LDAP_PORT;
        dnsuffix = "";
@@ -316,19 +290,21 @@ main(
        while (( c = getopt( argc, argv, "uh:d:s:p:t:T:" )) != -1 ) {
                switch( c ) {
                case 'u':
-#ifdef CLDAP
+#ifdef LDAP_CONNECTIONLESS
                        cldapflg++;
-#else /* CLDAP */
-                       printf( "Compile with -DCLDAP for UDP support\n" );
-#endif /* CLDAP */
+#else /* LDAP_CONNECTIONLESS */
+                       printf( "Compile with -DLDAP_CONNECTIONLESS for UDP support\n" );
+#endif /* LDAP_CONNECTIONLESS */
                        break;
 
                case 'd':
 #ifdef LDAP_DEBUG
                        ldap_debug = atoi( optarg );
+#ifdef LBER_DEBUG
                        if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
                                lber_debug = ldap_debug;
                        }
+#endif
 #else
                        printf( "Compile with -DLDAP_DEBUG for debugging\n" );
 #endif
@@ -346,7 +322,6 @@ main(
                        port = atoi( optarg );
                        break;
 
-#if !defined(MACOS) && !defined(DOS)
                case 't':       /* copy ber's to given file */
                        copyfname = strdup( optarg );
                        copyoptions = LBER_TO_FILE;
@@ -356,7 +331,6 @@ main(
                        copyfname = strdup( optarg );
                        copyoptions = (LBER_TO_FILE | LBER_TO_FILE_ONLY);
                        break;
-#endif
 
                default:
                    ++errflg;
@@ -377,9 +351,9 @@ main(
                host == NULL ? "(null)" : host, port );
 
        if ( cldapflg ) {
-#ifdef CLDAP
+#ifdef LDAP_CONNECTIONLESS
                ld = cldap_open( host, port );
-#endif /* CLDAP */
+#endif /* LDAP_CONNECTIONLESS */
        } else {
                ld = ldap_open( host, port );
        }
@@ -389,7 +363,6 @@ main(
                exit(1);
        }
 
-#if !defined(MACOS) && !defined(DOS)
        if ( copyfname != NULL ) {
                if ( (ld->ld_sb.sb_fd = open( copyfname, O_WRONLY | O_CREAT,
                    0600 ))  == -1 ) {
@@ -398,7 +371,6 @@ main(
                }
                ld->ld_sb.sb_options = copyoptions;
        }
-#endif
 
        bound = 0;
        timeout.tv_sec = 0;
@@ -440,13 +412,13 @@ main(
                        break;
 
                case 'b':       /* asynch bind */
-#ifdef KERBEROS
+#ifdef HAVE_KERBEROS
                        getline( line, sizeof(line), stdin,
                            "method (0->simple, 1->krbv41, 2->krbv42)? " );
                        method = atoi( line ) | 0x80;
-#else /* KERBEROS */
+#else /* HAVE_KERBEROS */
                        method = LDAP_AUTH_SIMPLE;
-#endif /* KERBEROS */
+#endif /* HAVE_KERBEROS */
                        getline( dn, sizeof(dn), stdin, "dn? " );
                        strcat( dn, dnsuffix );
 
@@ -466,7 +438,7 @@ main(
                        break;
 
                case 'B':       /* synch bind */
-#ifdef KERBEROS
+#ifdef HAVE_KERBEROS
                        getline( line, sizeof(line), stdin,
                            "method 0->simple 1->krbv41 2->krbv42 3->krb? " );
                        method = atoi( line );
@@ -474,9 +446,9 @@ main(
                                method = LDAP_AUTH_KRBV4;
                        else
                                method = method | 0x80;
-#else /* KERBEROS */
+#else /* HAVE_KERBEROS */
                        method = LDAP_AUTH_SIMPLE;
-#endif /* KERBEROS */
+#endif /* HAVE_KERBEROS */
                        getline( dn, sizeof(dn), stdin, "dn? " );
                        strcat( dn, dnsuffix );
 
@@ -512,9 +484,11 @@ main(
 #ifdef LDAP_DEBUG
                        getline( line, sizeof(line), stdin, "debug level? " );
                        ldap_debug = atoi( line );
+#ifdef LBER_DEBUG
                        if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
                                lber_debug = ldap_debug;
                        }
+#endif
 #else
                        printf( "Compile with -DLDAP_DEBUG for debugging\n" );
 #endif
@@ -567,15 +541,15 @@ main(
                        break;
 
                case 'q':       /* quit */
-#ifdef CLDAP
+#ifdef LDAP_CONNECTIONLESS
                        if ( cldapflg )
                                cldap_close( ld );
-#endif /* CLDAP */
-#ifdef LDAP_REFERRALS
+#endif /* LDAP_CONNECTIONLESS */
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
                        if ( !cldapflg )
-#else /* LDAP_REFERRALS */
+#else /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
                        if ( !cldapflg && bound )
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
                                ldap_unbind( ld );
                        exit( 0 );
                        break;
@@ -636,7 +610,7 @@ main(
                        attrsonly = atoi( line );
 
                        if ( cldapflg ) {
-#ifdef CLDAP
+#ifdef LDAP_CONNECTIONLESS
                            getline( line, sizeof(line), stdin,
                                "Requestor DN (for logging)? " );
                            if ( cldap_search_s( ld, dn, scope, filter, types,
@@ -648,7 +622,7 @@ main(
                                handle_result( ld, res );
                                res = NULLMSG;
                            }
-#endif /* CLDAP */
+#endif /* LDAP_CONNECTIONLESS */
                        } else {
                            if (( id = ldap_search( ld, dn, scope, filter,
                                    types, attrsonly  )) == -1 ) {
@@ -749,9 +723,9 @@ main(
                        break;
 
                case 'e':       /* enable cache */
-#ifdef NO_CACHE
+#ifdef LDAP_NOCACHE
                        printf( NOCACHEERRMSG );
-#else /* NO_CACHE */
+#else /* LDAP_NOCACHE */
                        getline( line, sizeof(line), stdin, "Cache timeout (secs)? " );
                        i = atoi( line );
                        getline( line, sizeof(line), stdin, "Maximum memory to use (bytes)? " );
@@ -760,25 +734,25 @@ main(
                        } else {
                                printf( "ldap_enable_cache failed\n" ); 
                        }
-#endif /* NO_CACHE */
+#endif /* LDAP_NOCACHE */
                        break;
 
                case 'x':       /* uncache entry */
-#ifdef NO_CACHE
+#ifdef LDAP_NOCACHE
                        printf( NOCACHEERRMSG );
-#else /* NO_CACHE */
+#else /* LDAP_NOCACHE */
                        getline( line, sizeof(line), stdin, "DN? " );
                        ldap_uncache_entry( ld, line );
-#endif /* NO_CACHE */
+#endif /* LDAP_NOCACHE */
                        break;
 
                case 'X':       /* uncache request */
-#ifdef NO_CACHE
+#ifdef LDAP_NOCACHE
                        printf( NOCACHEERRMSG );
-#else /* NO_CACHE */
+#else /* LDAP_NOCACHE */
                        getline( line, sizeof(line), stdin, "request msgid? " );
                        ldap_uncache_request( ld, atoi( line ));
-#endif /* NO_CACHE */
+#endif /* LDAP_NOCACHE */
                        break;
 
                case 'o':       /* set ldap options */
@@ -789,7 +763,7 @@ main(
                        getline( line, sizeof(line), stdin, "sizelimit?" );
                        ld->ld_sizelimit = atoi( line );
 
-                       ld->ld_options = 0;
+                       LDAP_BOOL_ZERO(&ld->ld_options);
 
 #ifdef STR_TRANSLATION
                        getline( line, sizeof(line), stdin,
@@ -810,32 +784,32 @@ main(
                        }
 #endif /* STR_TRANSLATION */
 
-#ifdef LDAP_DNS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
                        getline( line, sizeof(line), stdin,
                                "Use DN & DNS to determine where to send requests (0=no, 1=yes)?" );
                        if ( atoi( line ) != 0 ) {
-                               ld->ld_options |= LDAP_OPT_DNS;
+                               LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_DNS);
                        }
-#endif /* LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
 
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
                        getline( line, sizeof(line), stdin,
                                "Recognize and chase referrals (0=no, 1=yes)?" );
                        if ( atoi( line ) != 0 ) {
-                               ld->ld_options |= LDAP_OPT_REFERRALS;
+                               LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_REFERRALS);
                                getline( line, sizeof(line), stdin,
                                        "Prompt for bind credentials when chasing referrals (0=no, 1=yes)?" );
                                if ( atoi( line ) != 0 ) {
                                        ldap_set_rebind_proc( ld, bind_prompt );
                                }
                        }
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
                        break;
 
                case 'O':       /* set cache options */
-#ifdef NO_CACHE
+#ifdef LDAP_NOCACHE
                        printf( NOCACHEERRMSG );
-#else /* NO_CACHE */
+#else /* LDAP_NOCACHE */
                        getline( line, sizeof(line), stdin, "cache errors (0=smart, 1=never, 2=always)?" );
                        switch( atoi( line )) {
                        case 0:
@@ -852,7 +826,7 @@ main(
                        default:
                                printf( "not a valid cache option\n" );
                        }
-#endif /* NO_CACHE */
+#endif /* LDAP_NOCACHE */
                        break;
 
                case '?':       /* help */
@@ -956,13 +930,9 @@ print_search_entry( LDAP *ld, LDAPMessage *res )
 
                ufn = ldap_dn2ufn( dn );
                printf( "\tUFN: %s\n", ufn );
-#ifdef WINSOCK
-               ldap_memfree( dn );
-               ldap_memfree( ufn );
-#else /* WINSOCK */
+
                free( dn );
                free( ufn );
-#endif /* WINSOCK */
 
                for ( a = ldap_first_attribute( ld, e, &ber ); a != NULL;
                    a = ldap_next_attribute( ld, e, ber ) ) {
@@ -975,7 +945,7 @@ print_search_entry( LDAP *ld, LDAPMessage *res )
                                        int     j, nonascii;
 
                                        nonascii = 0;
-                                       for ( j = 0; j < vals[i]->bv_len; j++ )
+                                       for ( j = 0; (unsigned long) j < vals[i]->bv_len; j++ )
                                                if ( !isascii( vals[i]->bv_val[j] ) ) {
                                                        nonascii = 1;
                                                        break;
@@ -1001,23 +971,3 @@ print_search_entry( LDAP *ld, LDAPMessage *res )
            || res->lm_chain != NULLMSG )
                print_ldap_result( ld, res, "search" );
 }
-
-
-#ifdef WINSOCK
-void
-ldap_perror( LDAP *ld, char *s )
-{
-       char    *errs;
-
-       if ( ld == NULL ) {
-               perror( s );
-               return;
-       }
-
-       errs = ldap_err2string( ld->ld_errno );
-       printf( "%s: %s\n", s, errs == NULL ? "unknown error" : errs );
-       if ( ld->ld_error != NULL && *ld->ld_error != '\0' ) {
-               printf( "%s: additional info: %s\n", s, ld->ld_error );
-       }
-}
-#endif /* WINSOCK */