]> git.sur5r.net Git - openldap/blobdiff - tests/progs/slapd-common.c
ITS#8798 Unify slapd-* tools setup
[openldap] / tests / progs / slapd-common.c
index da513efeed56b0ca455cbece3012fbdd924598fd..81ce7e53c2a38197c50e59036395e04e61a137cb 100644 (file)
@@ -28,6 +28,7 @@
 
 #include "ldap.h"
 
+#include "lutil.h"
 #include "ldap_pvt.h"
 #include "slapd-common.h"
 
@@ -127,6 +128,9 @@ static const struct {
 
 #define UNKNOWN_ERR    (1234567890)
 
+#define RETRIES 0
+#define LOOPS  100
+
 static int
 tester_ignore_str2err( const char *err )
 {
@@ -197,27 +201,26 @@ tester_ignore_err( int err )
        return rc;
 }
 
-void
+struct tester_conn_args *
 tester_init( const char *pname, tester_t ptype )
 {
+       static struct tester_conn_args config = {
+               .authmethod = -1,
+               .retries = RETRIES,
+               .loops = LOOPS,
+               .outerloops = 1,
+
+               .uri = NULL,
+               .host = "localhost",
+               .port = 389,
+       };
+
        pid = getpid();
        srand( pid );
        snprintf( progname, sizeof( progname ), "%s PID=%d", pname, pid );
        progtype = ptype;
-}
-
-char *
-tester_uri( char *uri, char *host, int port )
-{
-       static char     uribuf[ BUFSIZ ];
 
-       if ( uri != NULL ) {
-               return uri;
-       }
-
-       snprintf( uribuf, sizeof( uribuf ), "ldap://%s:%d", host, port );
-
-       return uribuf;
+       return &config;
 }
 
 void
@@ -293,6 +296,163 @@ tester_perror( const char *fname, const char *msg )
                        msg ? msg : "" );
 }
 
+int
+tester_config_opt( struct tester_conn_args *config, char opt, char *optarg )
+{
+       switch ( opt ) {
+               case 'C':
+                       config->chaserefs++;
+                       break;
+
+               case 'D':
+                       config->binddn = strdup( optarg );
+                       break;
+
+               case 'd':
+                       {
+                               int debug;
+                               if ( lutil_atoi( &debug, optarg ) != 0 ) {
+                                       return -1;
+                               }
+
+                               if ( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
+                                       != LBER_OPT_SUCCESS )
+                               {
+                                       fprintf( stderr,
+                                               "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
+                               }
+
+                               if ( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
+                                       != LDAP_OPT_SUCCESS )
+                               {
+                                       fprintf( stderr,
+                                               "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
+                               }
+                               break;
+                       }
+
+               case 'H':
+                       config->uri = strdup( optarg );
+                       break;
+
+               case 'h':
+                       config->host = strdup( optarg );
+                       break;
+
+               case 'i':
+                       tester_ignore_str2errlist( optarg );
+                       break;
+
+               case 'L':
+                       if ( lutil_atoi( &config->outerloops, optarg ) != 0 ) {
+                               return -1;
+                       }
+                       break;
+
+               case 'l':
+                       if ( lutil_atoi( &config->loops, optarg ) != 0 ) {
+                               return -1;
+                       }
+                       break;
+
+               case 'p':
+                       if ( lutil_atoi( &config->port, optarg ) != 0 ) {
+                               return -1;
+                       }
+                       break;
+
+               case 'r':
+                       if ( lutil_atoi( &config->retries, optarg ) != 0 ) {
+                               return -1;
+                       }
+                       break;
+
+               case 't':
+                       if ( lutil_atoi( &config->delay, optarg ) != 0 ) {
+                               return -1;
+                       }
+                       break;
+
+               case 'w':
+                       config->pass.bv_val = strdup( optarg );
+                       config->pass.bv_len = strlen( optarg );
+                       memset( optarg, '*', config->pass.bv_len );
+                       break;
+
+               case 'x':
+                       if ( config->authmethod != -1 && config->authmethod != LDAP_AUTH_SIMPLE ) {
+                               return -1;
+                       }
+                       config->authmethod = LDAP_AUTH_SIMPLE;
+                       break;
+
+               default:
+                       return -1;
+       }
+
+       return LDAP_SUCCESS;
+}
+
+void
+tester_config_finish( struct tester_conn_args *config )
+{
+       if ( !config->uri ) {
+               static char     uribuf[ BUFSIZ ];
+
+               config->uri = uribuf;
+               snprintf( uribuf, sizeof( uribuf ), "ldap://%s:%d",
+                               config->host, config->port );
+       }
+
+       if ( config->authmethod == -1 ) {
+               config->authmethod = LDAP_AUTH_SIMPLE;
+       }
+}
+
+void
+tester_init_ld( LDAP **ldp, struct tester_conn_args *config, int flags )
+{
+       LDAP *ld;
+       int rc, do_retry = config->retries;
+       int version = LDAP_VERSION3;
+
+retry:;
+       ldap_initialize( &ld, config->uri );
+       if ( ld == NULL ) {
+               tester_perror( "ldap_initialize", NULL );
+               exit( EXIT_FAILURE );
+       }
+
+       (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
+       (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
+               config->chaserefs ? LDAP_OPT_ON: LDAP_OPT_OFF );
+
+       if ( !( flags & TESTER_INIT_ONLY ) ) {
+               rc = ldap_sasl_bind_s( ld,
+                               config->binddn, LDAP_SASL_SIMPLE,
+                               &config->pass, NULL, NULL, NULL );
+
+               if ( rc != LDAP_SUCCESS ) {
+                       tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
+                       switch ( rc ) {
+                               case LDAP_BUSY:
+                               case LDAP_UNAVAILABLE:
+                                       if ( do_retry > 0 ) {
+                                               do_retry--;
+                                               if ( config->delay > 0 ) {
+                                                       sleep( config->delay );
+                                               }
+                                               goto retry;
+                                       }
+                       }
+                       tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
+                       exit( EXIT_FAILURE );
+               }
+       }
+
+       *ldp = ld;
+}
+
 void
 tester_error( const char *msg )
 {