]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/tools/slapcommon.c
Changes from HEAD for beta
[openldap] / servers / slapd / tools / slapcommon.c
index 7cee79bbb63001f437b7eed634a5b4af6c149e2d..c0a2d47a9d3c54db41731aafca4f01855c55c62e 100644 (file)
@@ -1,6 +1,6 @@
 /* $OpenLDAP$ */
 /*
- * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
+ * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
  */
 /* slapcommon.c - common routine for the slap tools */
@@ -23,8 +23,13 @@ char *progname       = NULL;
 char   *conffile       = SLAPD_DEFAULT_CONFIGFILE;
 int            truncatemode = 0;
 int            verbose         = 0;
+int            update_ctxcsn = SLAP_TOOL_CTXCSN_NONE;
+int            retrieve_ctxcsn = 0;
+int            retrieve_synccookie = 0;
 int            continuemode = 0;
-int    nosubs          = 0;
+int            nosubordinates = 0;
+int            dryrun = 0;
+struct berval  sub_ndn = { 0, NULL };
 
 char   *ldiffile       = NULL;
 FILE   *ldiffp         = NULL;
@@ -46,11 +51,11 @@ usage( int tool )
 
        switch( tool ) {
        case SLAPADD:
-               options = "\t[-l ldiffile]\n";
+               options = "\t[-l ldiffile] [-u] [-W] [-w]\n";
                break;
 
        case SLAPCAT:
-               options = "\t[-l ldiffile]\n";
+               options = "\t[-l ldiffile] [-m] [-k]\n";
                break;
 
        case SLAPINDEX:
@@ -81,6 +86,7 @@ slap_tool_init(
 {
        char *options;
        struct berval base = { 0, NULL };
+       char *subtree = NULL;
        int rc, i, dbnum;
        int mode = SLAP_TOOL_MODE;
 
@@ -97,15 +103,17 @@ slap_tool_init(
 
        switch( tool ) {
        case SLAPADD:
-               options = "b:cd:f:l:n:tv";
+               options = "b:cd:f:l:n:tuvWw";
                break;
 
        case SLAPINDEX:
                options = "b:cd:f:n:v";
+               mode |= SLAP_TOOL_READMAIN;
                break;
 
        case SLAPCAT:
-               options = "b:cd:f:l:n:v";
+               options = "b:cd:f:kl:mn:s:v";
+               mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
                break;
 
        default:
@@ -121,7 +129,8 @@ slap_tool_init(
                switch ( i ) {
                case 'b':
                        base.bv_val = strdup( optarg );
-                       base.bv_len = strlen( optarg );
+                       base.bv_len = strlen( base.bv_val );
+                       break;
 
                case 'c':       /* enable continue mode */
                        continuemode++;
@@ -135,23 +144,48 @@ slap_tool_init(
                        conffile = strdup( optarg );
                        break;
 
+               case 'k':       /* Retrieve sync cookie entry */
+                       retrieve_synccookie = 1;
+                       break;
+
                case 'l':       /* LDIF file */
                        ldiffile = strdup( optarg );
                        break;
 
+               case 'm':       /* Retrieve ldapsync entry */
+                       retrieve_ctxcsn = 1;
+                       break;
+
                case 'n':       /* which config file db to index */
                        dbnum = atoi( optarg ) - 1;
                        break;
 
+               case 's':       /* dump subtree */
+                       subtree = strdup( optarg );
+                       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;
 
+               case 'W':       /* write context csn on every entry add */
+                       update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH;
+                       /* FIXME : update_ctxcsn = SLAP_TOOL_CTXCSN_ENTRY; */
+                       break;
+
+               case 'w':       /* write context csn on at the end */
+                       update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH;
+                       break;
+
                default:
                        usage( tool );
                        break;
@@ -176,21 +210,33 @@ 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 ) {
+       if ( rc != 0 ) {
                fprintf( stderr, "%s: slap_init failed!\n", progname );
                exit( EXIT_FAILURE );
        }
 
-       rc = schema_init();
+       rc = slap_schema_init();
 
-       if (rc != 0 ) {
+       if ( rc != 0 ) {
                fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
                exit( EXIT_FAILURE );
        }
 
-       read_config( conffile );
+       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" );
@@ -199,56 +245,101 @@ slap_tool_init(
 
        rc = glue_sub_init();
 
-       if (rc != 0 ) {
+       if ( rc != 0 ) {
                fprintf( stderr, "Subordinate configuration error\n" );
                exit( EXIT_FAILURE );
        }
 
-       rc = schema_prep();
+       rc = slap_schema_check();
 
-       if (rc != 0 ) {
+       if ( rc != 0 ) {
                fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
                exit( EXIT_FAILURE );
        }
 
+       if( subtree ) {
+               struct berval val;
+               val.bv_val = subtree;
+               val.bv_len = strlen( subtree );
+               rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
+               if( rc != LDAP_SUCCESS ) {
+                       fprintf( stderr, "Invalid subtree DN '%s'\n", optarg );
+                       exit( EXIT_FAILURE );
+               }
+
+               if( base.bv_val == NULL && dbnum == -1 )
+                       base = val;
+               else
+                       free( subtree );
+       }
+
        if( base.bv_val != NULL ) {
-               struct berval *nbase = NULL;
+               struct berval nbase;
 
-               rc = dnNormalize( NULL, &base, &nbase );
+               rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
                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( nbase->bv_val, 0, 0 );
-               ber_bvfree( nbase );
+               be = select_backend( &nbase, 0, 0 );
+               ber_memfree( nbase.bv_val );
 
                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_glueflags & SLAP_GLUE_INSTANCE)
-                       nosubs = 1;
+               if (SLAP_GLUE_INSTANCE(be)) {
+                       nosubordinates = 1;
+               }
 
        } else if ( dbnum == -1 ) {
+               if ( nbackends <= 0 ) {
+                       fprintf( stderr, "No available databases\n" );
+                       exit( EXIT_FAILURE );
+               }
+               
                be = &backends[dbnum=0];
                /* If just doing the first by default and it is a
                 * glue subordinate, find the master.
                 */
-               while (be->be_glueflags & SLAP_GLUE_SUBORDINATE) {
-                       nosubs = 1;
+               while (SLAP_GLUE_SUBORDINATE(be) || SLAP_MONITOR(be)) {
+                       if (SLAP_GLUE_SUBORDINATE(be)) {
+                               nosubordinates = 1;
+                       }
                        be++;
+                       dbnum++;
+               }
+
+
+               if ( dbnum >= nbackends ) {
+                       fprintf( stderr, "Available database(s) "
+                                       "do not allow %s\n", name );
+                       exit( EXIT_FAILURE );
+               }
+               
+               if ( nosubordinates == 0 && dbnum > 0 ) {
+#ifdef NEW_LOGGING
+                       LDAP_LOG( BACKEND, ERR, 
+"The first database does not allow %s; using the first available one (%d)\n",
+                               name, dbnum + 1, 0 );
+#else
+                       Debug( LDAP_DEBUG_ANY,
+"The first database does not allow %s; using the first available one (%d)\n",
+                               name, dbnum + 1, 0 );
+#endif
                }
 
        } 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 );
 
@@ -267,6 +358,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 );