]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/tools/slapcommon.c
Don't reeval expression
[openldap] / servers / slapd / tools / slapcommon.c
index 8b41f223233c4f3bd28490914dac2b177892427b..2004511a678686ba3d65a24c851042bde913aa5a 100644 (file)
@@ -1,6 +1,6 @@
 /* $OpenLDAP$ */
 /*
- * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
+ * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
  */
 /* slapcommon.c - common routine for the slap tools */
@@ -23,8 +23,9 @@ char  *progname       = NULL;
 char   *conffile       = SLAPD_DEFAULT_CONFIGFILE;
 int            truncatemode = 0;
 int            verbose         = 0;
-int            noschemacheck = 0;
 int            continuemode = 0;
+int            nosubordinates = 0;
+int            dryrun = 0;
 
 char   *ldiffile       = NULL;
 FILE   *ldiffp         = NULL;
@@ -42,11 +43,11 @@ usage( int tool )
        char *options = NULL;
        fprintf( stderr,
                "usage: %s [-v] [-c] [-d debuglevel] [-f configfile]\n"
-                        "\t[-n databasenumber | -b suffix]", progname );
+                       "\t[-n databasenumber | -b suffix]", progname );
 
        switch( tool ) {
        case SLAPADD:
-               options = "\t[-s] [-l ldiffile]\n";
+               options = "\t[-l ldiffile]\n";
                break;
 
        case SLAPCAT:
@@ -54,7 +55,7 @@ usage( int tool )
                break;
 
        case SLAPINDEX:
-               options = "\tattributetype\n";
+               options = "\n";
                break;
        }
 
@@ -80,7 +81,7 @@ slap_tool_init(
        int argc, char **argv )
 {
        char *options;
-       char *base = NULL;
+       struct berval base = { 0, NULL };
        int rc, i, dbnum;
        int mode = SLAP_TOOL_MODE;
 
@@ -97,7 +98,7 @@ slap_tool_init(
 
        switch( tool ) {
        case SLAPADD:
-               options = "b:cd:f:l:n:stv";
+               options = "b:cd:f:l:n:tuv";
                break;
 
        case SLAPINDEX:
@@ -120,7 +121,8 @@ slap_tool_init(
        while ( (i = getopt( argc, argv, options )) != EOF ) {
                switch ( i ) {
                case 'b':
-                       base = strdup( optarg );
+                       base.bv_val = strdup( optarg );
+                       base.bv_len = strlen( base.bv_val );
 
                case 'c':       /* enable continue mode */
                        continuemode++;
@@ -142,15 +144,15 @@ slap_tool_init(
                        dbnum = atoi( optarg ) - 1;
                        break;
 
-               case 's':       /* disable schema checking */
-                       noschemacheck++;
-                       break;
-
                case 't':       /* turn on truncate */
                        truncatemode++;
                        mode |= SLAP_TRUNCATE_MODE;
                        break;
 
+               case 'u':       /* dry run */
+                       dryrun++;
+                       break;
+
                case 'v':       /* turn on verbose */
                        verbose++;
                        break;
@@ -161,9 +163,7 @@ slap_tool_init(
                }
        }
 
-       if ( ( argc != optind + (tool == SLAPINDEX ? 1 : 0) )
-               || (dbnum >= 0 && base != NULL ) )
-       {
+       if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
                usage( tool );
        }
 
@@ -181,6 +181,13 @@ slap_tool_init(
         * initialize stuff and figure out which backend we're dealing with
         */
 
+#ifdef SLAPD_MODULES
+       if ( module_init() != 0 ) {
+               fprintf( stderr, "%s: module_init failed!\n", progname );
+               exit( EXIT_FAILURE );
+       }
+#endif
+               
        rc = slap_init( mode, progname );
 
        if (rc != 0 ) {
@@ -188,38 +195,78 @@ slap_tool_init(
                exit( EXIT_FAILURE );
        }
 
-       read_config( conffile );
+       rc = slap_schema_init();
+
+       if (rc != 0 ) {
+               fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
+               exit( EXIT_FAILURE );
+       }
+
+       rc = read_config( conffile, 0 );
+
+       if ( rc != 0 ) {
+               fprintf( stderr, "%s: bad configuration file!\n", progname );
+               exit( EXIT_FAILURE );
+       }
 
        if ( !nbackends ) {
                fprintf( stderr, "No databases found in config file\n" );
                exit( EXIT_FAILURE );
        }
 
-       if( base != NULL ) {
-               char *tbase = ch_strdup( base );
+       rc = glue_sub_init();
+
+       if (rc != 0 ) {
+               fprintf( stderr, "Subordinate configuration error\n" );
+               exit( EXIT_FAILURE );
+       }
+
+       rc = slap_schema_check();
 
-               if( dn_normalize_case( tbase ) == NULL ) {
+       if (rc != 0 ) {
+               fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
+               exit( EXIT_FAILURE );
+       }
+
+       if( base.bv_val != NULL ) {
+               struct berval *nbase = NULL;
+
+               rc = dnNormalize( NULL, &base, &nbase );
+               if( rc != LDAP_SUCCESS ) {
                        fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
-                               progname, base );
+                               progname, base.bv_val );
                        exit( EXIT_FAILURE );
                }
 
-               be = select_backend( tbase );
-               free( tbase );
+               be = select_backend( nbase, 0, 0 );
+               ber_bvfree( nbase );
 
                if( be == NULL ) {
                        fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
-                               progname, base );
+                               progname, base.bv_val );
                        exit( EXIT_FAILURE );
                }
+               /* If the named base is a glue master, operate on the
+                * entire context
+                */
+               if (be->be_flags & SLAP_BFLAG_GLUE_INSTANCE)
+                       nosubordinates = 1;
 
        } else if ( dbnum == -1 ) {
                be = &backends[dbnum=0];
+               /* If just doing the first by default and it is a
+                * glue subordinate, find the master.
+                */
+               while (be->be_flags & SLAP_BFLAG_GLUE_SUBORDINATE) {
+                       nosubordinates = 1;
+                       be++;
+               }
 
        } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
                fprintf( stderr,
                        "Database number selected via -n is out of range\n"
-                       "Must be in the range 1 to %d (number of databases in the config file)\n",
+                       "Must be in the range 1 to %d"
+                               " (number of databases in the config file)\n",
                        nbackends );
                exit( EXIT_FAILURE );
 
@@ -238,6 +285,18 @@ void slap_tool_destroy( void )
 {
        slap_shutdown( be );
        slap_destroy();
+#ifdef SLAPD_MODULES
+       if ( slapMode == SLAP_SERVER_MODE ) {
+       /* always false. just pulls in necessary symbol references. */
+               lutil_uuidstr(NULL, 0);
+       }
+       module_kill();
+#endif
+       schema_destroy();
+#ifdef HAVE_TLS
+       ldap_pvt_tls_destroy();
+#endif
+       config_destroy();
 
 #ifdef CSRIMALLOC
        mal_dumpleaktrace( leakfile );